Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL count on several elements that might not be there 1

Status
Not open for further replies.

lhg1

IS-IT--Management
Mar 29, 2005
134
DK
Hi

I need to do an SQL that count the number of elements in a table, and if it isn't there it shoul say 0

ex on the sql I using now
Code:
SELECT   phd_id, count(*) Count
FROM     q3
WHERE    dvc_trx_sts_cd = 'PE'
and phd_id in ('ABMSR01','KDHLR01','KDHLR02')
GROUP BY phd_id;

the result is
phd_id count
ABMSR01 1433

And should be
phd_id count
ABMSR01 1433
KDHLR01 0
KDHLR02 0


Hope someone can help

/LHG
 
Provided you have a table containing all the possible phd:
SELECT phd_id
,(SELECT COUNT(*) FROM q3 WHERE dvc_trx_sts_cd='PE' AND phd_id=A.phd_id) Count
FROM your_phd_table A
WHERE phd_id IN ('ABMSR01','KDHLR01','KDHLR02')
GROUP BY phd_id;

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV

Your solution worked perfect.

/Lhg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top