Reputation: 13
I have a table given below
id album_id album_name ---- --------- ----------- 1 5 test1 2 5 test1 3 6 test3 4 6 test3 5 NULL test4 6 NULL test4
I want to write a query using group by album_id , it will give below result
id album_id album_name ---- --------- ----------- 1 5 test1 3 6 test3 5 NULL test4 6 NULL test4
I have tried but its grouping NULL column also, I want all the rows which are NULL and group by album_id which are not null.
I have followed this link - but don't work for me
Thanks
Upvotes: 1
Views: 8110
Reputation: 69769
You could use this:
SELECT MIN(id) AS id,
album_id,
album_name
FROM T
GROUP BY COALESCE(album_id, id), album_name
ORDER BY album_name;
So when album_id
IS NULL then you also group by id
ensuring that all rows are returned where album_id
is null.
Upvotes: 5