mattmontalto
IS-IT--Management
I once found a genius on this forum who helped me with a complicated query I needed.... Now I am looking for another.... I have created a DB that has 3 tables, main_tbl, victim_tbl & suspect_tbl.
I then have a query that is selecting all fields of all tables, mainly so that every field is searchable via the web application I am setting up for the users.
The problem is with the results returned from this large query... For each record in the main_tbl, there is one case # (called scsv_no) and it is returned multiple times... one time for each victim and suspect in the child tables... So if there are two victims and two suspects that are related to one scsv_no, then I see that scsv_no returned four times in the main query's results.
I am trying to explore fixing this using the "GROUP BY" function, but I need a little guidance... any help would be greatly appreciated... Pasted below is the actual query. Thanks in advance.
SELECT
dbo.main_tbl.id,
dbo.main_tbl.scsv_no,
dbo.main_tbl.datereceived,
dbo.main_tbl.CaseOrigin,
dbo.main_tbl.CaseOrigin_Other,
dbo.main_tbl.OrigADA,
dbo.main_tbl.OrigADA_active,
dbo.main_tbl.ADA1,
dbo.main_tbl.ADA1_active,
dbo.main_tbl.ADA2,
dbo.main_tbl.ADA2_active,
dbo.main_tbl.ChldAbsRef,
dbo.main_tbl.Docket,
dbo.main_tbl.fileno,
dbo.main_tbl.IndSci,
dbo.main_tbl.JD,
dbo.main_tbl.IDV,
dbo.main_tbl.Charge,
dbo.main_tbl.AbuseType1,
dbo.main_tbl.AbuseType1_other,
dbo.main_tbl.AbuseType2,
dbo.main_tbl.AbuseType2_other,
dbo.main_tbl.dviolence,
dbo.main_tbl.DateofOccurStart,
dbo.main_tbl.DateofOccurEnd,
dbo.main_tbl.LocAdd1,
dbo.main_tbl.LocAdd2,
dbo.main_tbl.City,
dbo.main_tbl.[State],
dbo.main_tbl.Zip,
dbo.main_tbl.CaseWorker,
dbo.main_tbl.VicAdv,
dbo.main_tbl.VicAdv_active,
dbo.main_tbl.Detective,
dbo.main_tbl.Det_Active,
dbo.main_tbl.Disposition,
dbo.main_tbl.Sentence,
dbo.main_tbl.Notes,
dbo.main_tbl.RelatedToCaseNo,
dbo.main_tbl.CaseOpen,
dbo.main_tbl.CaseClosed,
dbo.main_tbl.DateClosed,
dbo.victim_tbl.id AS id1,
dbo.victim_tbl.main_tbl_id,
dbo.victim_tbl.[Last],
dbo.victim_tbl.[First],
dbo.victim_tbl.sex,
dbo.victim_tbl.dob,
dbo.victim_tbl.age,
dbo.victim_tbl.race,
dbo.victim_tbl.race_other,
dbo.suspect_tbl.id AS id2,
dbo.suspect_tbl.main_tbl_id AS main_tbl_id1,
dbo.suspect_tbl.[Last] AS Last1,
dbo.suspect_tbl.[First] AS First1,
dbo.suspect_tbl.sex AS sex1,
dbo.suspect_tbl.dob AS dob1,
dbo.suspect_tbl.age AS age1,
dbo.suspect_tbl.race AS race1,
dbo.suspect_tbl.race_other AS race_other1,
dbo.suspect_tbl.RelationtoVictim,
dbo.suspect_tbl.RelationtoVictim_other
FROM dbo.main_tbl
LEFT OUTER JOIN dbo.victim_tbl ON dbo.main_tbl.id = dbo.victim_tbl.main_tbl_id
LEFT OUTER JOIN dbo.suspect_tbl ON dbo.main_tbl.id = dbo.suspect_tbl.main_tbl_id
ORDER BY dbo.main_tbl.scsv_no DESC, dbo.main_tbl.id DESC
I then have a query that is selecting all fields of all tables, mainly so that every field is searchable via the web application I am setting up for the users.
The problem is with the results returned from this large query... For each record in the main_tbl, there is one case # (called scsv_no) and it is returned multiple times... one time for each victim and suspect in the child tables... So if there are two victims and two suspects that are related to one scsv_no, then I see that scsv_no returned four times in the main query's results.
I am trying to explore fixing this using the "GROUP BY" function, but I need a little guidance... any help would be greatly appreciated... Pasted below is the actual query. Thanks in advance.
SELECT
dbo.main_tbl.id,
dbo.main_tbl.scsv_no,
dbo.main_tbl.datereceived,
dbo.main_tbl.CaseOrigin,
dbo.main_tbl.CaseOrigin_Other,
dbo.main_tbl.OrigADA,
dbo.main_tbl.OrigADA_active,
dbo.main_tbl.ADA1,
dbo.main_tbl.ADA1_active,
dbo.main_tbl.ADA2,
dbo.main_tbl.ADA2_active,
dbo.main_tbl.ChldAbsRef,
dbo.main_tbl.Docket,
dbo.main_tbl.fileno,
dbo.main_tbl.IndSci,
dbo.main_tbl.JD,
dbo.main_tbl.IDV,
dbo.main_tbl.Charge,
dbo.main_tbl.AbuseType1,
dbo.main_tbl.AbuseType1_other,
dbo.main_tbl.AbuseType2,
dbo.main_tbl.AbuseType2_other,
dbo.main_tbl.dviolence,
dbo.main_tbl.DateofOccurStart,
dbo.main_tbl.DateofOccurEnd,
dbo.main_tbl.LocAdd1,
dbo.main_tbl.LocAdd2,
dbo.main_tbl.City,
dbo.main_tbl.[State],
dbo.main_tbl.Zip,
dbo.main_tbl.CaseWorker,
dbo.main_tbl.VicAdv,
dbo.main_tbl.VicAdv_active,
dbo.main_tbl.Detective,
dbo.main_tbl.Det_Active,
dbo.main_tbl.Disposition,
dbo.main_tbl.Sentence,
dbo.main_tbl.Notes,
dbo.main_tbl.RelatedToCaseNo,
dbo.main_tbl.CaseOpen,
dbo.main_tbl.CaseClosed,
dbo.main_tbl.DateClosed,
dbo.victim_tbl.id AS id1,
dbo.victim_tbl.main_tbl_id,
dbo.victim_tbl.[Last],
dbo.victim_tbl.[First],
dbo.victim_tbl.sex,
dbo.victim_tbl.dob,
dbo.victim_tbl.age,
dbo.victim_tbl.race,
dbo.victim_tbl.race_other,
dbo.suspect_tbl.id AS id2,
dbo.suspect_tbl.main_tbl_id AS main_tbl_id1,
dbo.suspect_tbl.[Last] AS Last1,
dbo.suspect_tbl.[First] AS First1,
dbo.suspect_tbl.sex AS sex1,
dbo.suspect_tbl.dob AS dob1,
dbo.suspect_tbl.age AS age1,
dbo.suspect_tbl.race AS race1,
dbo.suspect_tbl.race_other AS race_other1,
dbo.suspect_tbl.RelationtoVictim,
dbo.suspect_tbl.RelationtoVictim_other
FROM dbo.main_tbl
LEFT OUTER JOIN dbo.victim_tbl ON dbo.main_tbl.id = dbo.victim_tbl.main_tbl_id
LEFT OUTER JOIN dbo.suspect_tbl ON dbo.main_tbl.id = dbo.suspect_tbl.main_tbl_id
ORDER BY dbo.main_tbl.scsv_no DESC, dbo.main_tbl.id DESC