Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in SQL command using Group by?

I'm beginner in Oracle SQL. I am using SQL Developer. This query is not executed. I need for each id like 1001,1002 how many no of yes status and how many no of no status. Thanks in advance....

I used this SQL:

SELECT ID, COUNT(STATUS) 
FROM TABLE1 
WHERE 
GROUP BY ID, STATUS 
HAVING STATUS = YES OR STATUS = NO;

I have table like this:

id      school    status
--------------------------
1001    vani         YES
1002    sunbeam      YES
1001    shristri     YES
1002    jain         NO
1001    holycross    YES
1001    vani         NO

I need output like

id    yesstatus   Nostatus
-------------------------
1001      3        1
1002      1        1
like image 722
VIGNESH R Avatar asked Sep 11 '26 08:09

VIGNESH R


1 Answers

Your current query syntax is really wrong, but you can do conditional aggregation :

select id, 
       sum(case when status = 'YES' then 1 else 0 end) as yesstatus,
       sum(case when status = 'NO' then 1 else 0 end) as Nostatus      
from table1 t1
where status in ('YES', 'NO')
group by id;
like image 63
Yogesh Sharma Avatar answered Sep 13 '26 23:09

Yogesh Sharma