I’m getting duplicates when I do two LEFT JOINs to get to the “event_name” in my example below. I get 112 cases with it set up this way. However, if I get rid of the 2 LEFT JOIN lines and run the query, I get the proper 100 records without duplicates. I tried DISTINCT with the code below, but I still get 112 with duplicates.
SELECT "cases"."id", "cases"."date", "cases"."name", "event"."event_name"
FROM "cases"
LEFT JOIN "middle_table" ON "cases"."serial" = "middle_table"."m_serial"
LEFT JOIN "event" ON "middle_table"."e_serial" = "event"."ev_serial"
WHERE "cases"."date" BETWEEN '2012-12-11' AND '2012-12-13'
How can I specify that I only want the exact 100 cases from “cases”, and that I don’t want anything from the tables in the joins to produce any more rows?
Thanks!
You need to extend your ON clauses to include a condition so that for each entry in
casesthere is only one entry inmiddle_tablethat matches the condition and that for each entry inmiddle_tablethere is only one entry inevent:You can of course use DISTINCT. If that doesn’t work it means that your results are all different in the fields
cases.id,cases.date,cases.nameandevent.event_name. Examine the results and decide which of the entries you want to throw away and include that condition in your ON clause.