I'm trying to query a DB2 AS400/iSeries database from Delphi, but I'm pretty sure it's an SQL problem (and you guys are the best at solving SQL problems!!!). I have the following tables:
I am trying to get a totals query counting the number of jurors for each status code. But, I want all the status codes included in the result whether or not a juror has been assigned that code for the specified term.
So for the examples above, I would like to return:
I have tried:
which I expected to return all the records from JMPSTATUS, but it doesn't it returns (actual data):
but this listing is missing status codes AM and EC, EF (which have a zero total for the term date selected)
Can I not use the LEFT JOIN operation to get the expected results when using an aggregate function? I have tried selecting from JMPSTATUS and joining into JMPMAIN, I have used Right Joins, added and removed the OUTER keyword. I just don't know!!!
Thanks for any insight!
Leslie
Code:
[b]JMPSTATUS[/b]
STCODE STDESC
WS Will Serve
PP Postponed
IC Ineligible Citizen
EM Excused Medical
[b]JMPMAIN[/b]
JURNUM etc.... STATUSCD ...... ORGPOST
12345 WS 20050110
12346 PP 20050110
12347 EM 20050110
12348 WS 20050110
12349 PP 20050110
12350 WS 20050110
I am trying to get a totals query counting the number of jurors for each status code. But, I want all the status codes included in the result whether or not a juror has been assigned that code for the specified term.
So for the examples above, I would like to return:
Code:
WS 3
PP 2
IC (blank or 0)
EM 1
Code:
SELECT STCODE, COUNT(*) FROM JMPMAIN M LEFT OUTER JOIN JMPSTATUS S ON S.STCODE = M.STATUSCD WHERE ORGPOST = '20050110' GROUP BY STCODE
which I expected to return all the records from JMPSTATUS, but it doesn't it returns (actual data):
Code:
NR 11
PS 5
PP 16
RU 27
ES 1
EH 3
EM 19
IR 7
IC 5
IF 6
OT 4
WS 49
but this listing is missing status codes AM and EC, EF (which have a zero total for the term date selected)
Can I not use the LEFT JOIN operation to get the expected results when using an aggregate function? I have tried selecting from JMPSTATUS and joining into JMPMAIN, I have used Right Joins, added and removed the OUTER keyword. I just don't know!!!
Thanks for any insight!
Leslie