1

This is my table

SQL Table

I need to write a query for getting a dataset like below.(Avoid null fields and group by id) enter image description here

Please give me direction....

1 Answer 1

5

Try grouping by the id:

SELECT id,
       MAX(col1) AS col1,
       MAX(col2) AS col2,
       MAX(col3) AS col3,
       MAX(col4) AS col4
FROM test
GROUP BY id

The reason this works is that in SQL Server MAX ignores NULL values, so only the single VARCHAR value (e.g. 'ABC') will be retained during the grouping operation.

The data in your test table appear to be the intermediate step of a pivot query.

Sign up to request clarification or add additional context in comments.

2 Comments

@weeraa please mark as answer if that helped to resolve your issue
@jacky : Done. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.