mysql - Selecting a count on two separate conditions -


let's have following data.

id  name_id   completed 1    10          1 2    10          0 3    15          1 4    10          0 5    20          1 6    15          0 7    20          1  8    15          0  

i'm trying find count name id, pretty simple

select name_id, count(*) db  group name_id 

now, have second component want include in query.

for name_id 10, want count values completed 1. other name_id's, want select them regardless of whether 0 or 1.

so should end with:

name_id     count(*) 10            1 15            3 20            2 

name_id 10 has count of 1 because it's 1 completed, while other counts include both 0 , 1.

can task.

thanks!

you can use case expression inside of aggregate function.

select name_id,    sum(case            when name_id = 10            case when completed = 1 1 else 0 end           else 1 end)  total db  group name_id; 

see sql fiddle demo.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -