Posted: Wed Jul 22, 2009 12:13 pm Post subject: SQL output using Groupby statement
Hi,
I have a problem regarding SQL query, I am not getting the expected output, if the where condition is not satisfied the count should be displayed as zero. Could you please help me.
TABLE VALUES:
Code:
AC_NO RLE_CD
A00R16 GOOD
A00R16 BAD
A00R16 GOOD
B00R17 BAD
C00R19 GOOD
C00R19 BAD
D00R20 GOOD
D00R20 BAD
E00R22 GOOD
E00R22 BAD
sql is
Code:
SELECT AC_NO, COUNT (*)
FROM TABLEA WHERE AC_NO IN (
'A00R16',
'B00R17',
'C00R19',
'D00R20',
'E00R22')
AND RLE_CD = 'GOOD'
GROUP BY AC_NO ;;
Joined: 26 Nov 2002 Posts: 12372 Topics: 75 Location: San Jose
Posted: Wed Jul 22, 2009 12:44 pm Post subject:
vijayakumar,
Try this untested sql
Code:
SELECT AC_NO
,SUM(CASE WHEN RLE_CD = 'GOOD'
THEN INT(1) ELSE INT(0) END) AS COUNT
FROM TABLE
WHERE AC_NO IN ('A00R16',
'B00R17',
'C00R19',
'D00R20',
'E00R22')
GROUP BY AC_NO;
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum