question

AmeshJeram avatar image
AmeshJeram asked

grouping rows using t-sql when other columns are blank

CREATE TABLE #test
(
    PurchaseKey VARCHAR(100),
    ItemKey VARCHAR(100),
    column1 VARCHAR(100),
    column2 VARCHAR(100)
)

INSERT INTO #test (PurchaseKey, ItemKey, column1, column2)
    SELECT '52267434', '106502', NULL, '26405'
    UNION ALL
    SELECT '52267434', '106502', NULL, '26337'
    UNION ALL
    SELECT '52267434', '106502', '26337', NULL
    UNION ALL
    SELECT '52267434', '106502', '26405', NULL

SELECT * FROM #test
In the code above - I am able to generate an output with 4 rows.
But I require the data to become 2 rows only and basically the nulls fall away because the data merges to fill up the null values.
In this case Grouping will not work. Result should look like: 
| 52267434 106502 26337 26405 | 
| 52267434 106502 26405 26337 |
tsqlsql querymssqlgrouping
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

0 Answers

·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.