I have to do an self join on a table. I am trying to return a list of several columns to see how many of each type of drug test was performed on same day (MM/DD/YYYY) in which there were at least two tests done and at least one of which resulted in a result code of 'UN'.
I am joining other tables to get the information as below. The problem is I do not quite understand how to exclude someone who has a single result row in which they did have a 'UN' result on a day but did not have any other tests that day.
Query Results (Columns)
County, DrugTestID, ID, Name, CollectionDate, DrugTestType, Results, Count(DrugTestType)
I have several rows for ID 12345 which are correct. But ID 12346 is a single row of which is showing they had a row result of count (1). They had a result of 'UN' on this day but they did not have any other tests that day. I want to exclude this.
I tried the following query
select c.desc as 'County', dt.pid as 'PID', dt.id as 'DrugTestID', p.id as 'ID', bio.FullName as 'Participant', CONVERT(varchar, dt.CollectionDate, 101) as 'CollectionDate', dtt.desc as 'Drug Test Type', dt.result as Result, COUNT(dt.dru_drug_test_type) as 'Count Of Test Type' from dbo.Test as dt with (nolock) join dbo.History as h on dt.pid = h.id join dbo.Participant as p on h.pid = p.id join BioData as bio on bio.id = p.id join County as c with (nolock) on p.CountyCode = c.code join DrugTestType as dtt with (nolock) on dt.DrugTestType = dtt.code inner join ( select distinct dt2.pid, CONVERT(varchar, dt2.CollectionDate, 101) as 'CollectionDate' from dbo.DrugTest as dt2 with (nolock) join dbo.History as h2 on dt2.pid = h2.id join dbo.Participant as p2 on h2.pid = p2.id where dt2.result = 'UN' and dt2.CollectionDate between '11-01-2011' and '10-31-2012' and p2.DrugCourtType = 'AD' ) as derived on dt.pid = derived.pid and convert(varchar, dt.CollectionDate, 101) = convert(varchar, derived.CollectionDate, 101) group by c.desc, dt.pid, p.id, dt.id, bio.fullname, dt.CollectionDate, dtt.desc, dt.result order by c.desc ASC, Participant ASC, dt.CollectionDate ASC