I have a pretty long query which includes subqueries and joins that I need to run for a report. It runs without error, but the results show 11 rows for each value, when I need it to display 1 row per value. I'm using 'select distinct', which is what I thought I needed to use to accomplish this goal, but it doesn't seem to be working. What am I doing wrong?
The value that I need to display only 1 row per each distinct occurrence is "cases.casenum".
Here's the query:
SELECT distinct
(
SELECT count(distinct cases.casenum)
FROM (cases INNER JOIN user_case_data ON cases.casenum=user_case_data.casenum)
WHERE (user_case_data.discharged_date >= '##START##'
AND user_case_data.discharged_date <= '##END##')
)
AS TOTAL_LOST_FILES, cases.casenum AS 'Case Number', user_case_data.discharged_date AS 'Discharged Date',
(case when case_notes.topic like 'LOS Case Status Update' THEN case_notes.note_date else null end)
AS GENDOC_31_Mailed,
(case when case_checklist.code='101' then case_checklist.due_date else null end)
AS ADVISED_ATTORNEY,
(case when case_notes.topic like 'LOS Updated Lein Ltr' THEN case_notes.note_date else null end)
AS "Sent Updated Lien Ltr",
(case when case_checklist.code='109' then case_checklist.due_date else null end)
AS "Time Allocation Completed",
(case when case_checklist.code='110' then case_checklist.due_date else null end)
AS "Attorney Signed Affidavit",
(case when case_checklist.code='111' then case_checklist.due_date else null end)
AS "Lien Letters Sent",
(case when case_checklist.code='112' then case_checklist.due_date else null end)
AS "Sent Lien to Counsel",
(case when case_checklist.code='113' then case_checklist.due_date else null end)
AS "Received Costs and Transferred"
FROM (cases LEFT JOIN case_checklist ON cases.casenum=case_checklist.case_id)
LEFT JOIN user_case_data ON case_checklist.case_id=user_case_data.casenum
LEFT JOIN case_notes ON user_case_data.casenum=case_notes.case_num
WHERE (user_case_data.discharged_date >= '##START##'
AND user_case_data.discharged_date <= '##END##')
ORDER BY user_case_data.discharged_date ASC;
And here's an example of the output... which makes it easy to see what we're going for- no white space, just one row for each specific value in the "case number" column.
case
will have more than 1 entries incase_checklist
, and most likely will have more than 1 entries incase_notes
, thus the multiple rows. It's the nature of 1-* relationship. If you only want distinct case, then don't include those tables.case
anduser_case_data
? 1-1 or 1-*?