1

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

0

3 Answers 3

5

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;

Example on SQL Fiddle

So when album_id IS NULL then you also group by id ensuring that all rows are returned where album_id is null.

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

Comments

-1

try this

 SELECT * FROM <tablename>
 GROUP BY album_id,id;

Comments

-2
select * from YourTableName
group by album_id asc

try this..

1 Comment

First, it's not obvious what is OP's query. Second, null isn't a string value. It's a special value in DBMS. And like isn't a good way to deal with it

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.