views:

167

answers:

2

I have a table with the following columns:

SignatureID
PatientID
PatientVisitID
TreatAuthDate
HIPAADate
DrugTestDate

Now I have the following Select statement:

SELECT *
FROM tblSignature
WHERE PatientID = 12345

This select statement returns 8 rows. What I need to accomplish is getting the MAX TreatAuthDate - and with that MAX TreatAuthDate I need the PatientVisitID. Then I need the same type of information for the HipaaDate and DrugTestDate. How can I do this?

+10  A: 
SELECT  TOP 1 *
FROM    tblSignature 
WHERE   PatientID = 12345
ORDER BY
        TreatAuthDate DESC

To get three last results for different definitions of "last", use this:

SELECT  *
FROM    (
        SELECT  TOP 1 'LastThreatAuth' AS which, ts.*
        FROM    tblSignature ts
        WHERE   PatientID = 12345
        ORDER BY
                TreatAuthDate DESC
        ) SrcTreatAuth 
UNION ALL
SELECT  *
FROM    (
        SELECT  TOP 1 'LastHIPAA' AS which, ts.*
        FROM    tblSignature ts
        WHERE   PatientID = 12345
        ORDER BY
                HIPAADate DESC
        ) SrcHIPAA
UNION ALL
SELECT  *
FROM    (
        SELECT  TOP 1 'LastDrugTest' AS which, ts.*
        FROM    tblSignature ts
        WHERE   PatientID = 12345
        ORDER BY
                DrugTestDate DESC
        ) SrcDrugTest
Quassnoi
This does just what I need. Are there any suggestion how I could get the top 1 for each treatauthdate, hipaadate and drugtestdate? the top 1 for treatauth date may not be the top 1 for the drugtestdate, but I would need the patientvisitID for the top 1 treatauthdate... then the patientvisitID for the top 1 hipaadate... then the patientvisitID for the top1 drugtestdate. How could I put this in a stored procedure and us it to populate an object?
swolff1978
Perfect. I modified the above code adding SELECT * FROM ( ... ) SrcTreatAuth UNION ALL SELECT * FROM (...) SrcHipaa UNION ALL ...
swolff1978
@swolff1978: correct. I always forget I'm not in `Oracle` to omit query aliases.
Quassnoi
+1  A: 

SELECT patientid, max(Treatauthdate), max (HippaDAte) , max (DrugTestDate) FROM tblSignature WHERE PatientID = 12345 group by patientid

Note you can't ask for signatureid in this case as you would not filter any records out (I'm making the assumption signatureid is your PK). Further to get the max of each date per patient, it is likely they are each on a differnt row of the table so would not have the same signatureid.

To get the visit date for each type might be more difficult as each may be a separate visit. try something like

select a.patientid, Treatvisitdate, Treatauthdate,Hippavisitdate, HippaDate, DrugTestvisitdate, 
DrugTestDate
(SELECT  patientid, patientvisitdate as Treatvisitdate, max(Treatauthdate) as Treatauthdate
FROM tblSignature
WHERE PatientID = 12345
group by patientid,patientvisitdate)a
join
(SELECT  patientid, patientvisitdate as Hippavisitdate, max(HippaDate) as HippaDate
FROM tblSignature
WHERE PatientID = 12345
group by patientid,patientvisitdate) b on a.patientid = b.patientid
join
(SELECT  patientid, patientvisitdate as DrugTestvisitdate, max(DrugTestDate) as DrugTestDate
FROM tblSignature
WHERE PatientID = 12345
group by patientid,patientvisitdate) c on a.patientid = c.patientid

YOu might need left joins if some of the dates might not be in there.

HLGEM
Excellent! I "married" Quassnoi's and HLGEM's answers and got the results I was looking for - something along the lines of: select a.patientid, Treatvisitdate, Treatauthdate,Hippavisitdate, HippaDate, DrugTestvisitdate, DrugTestDate (SELECT TOP 1 patientid, patientvisitdate as Treatvisitdate, Treatauthdate as Treatauthdate FROM tblSignature WHERE PatientID = 12345 order by Treatauthdate )a join (SELECT TOP 1 patientid, patientvisitdate as Hippavisitdate, HippaDate as HippaDate FROM tblSignature WHERE PatientID = 12345 order by hippadate) b on a.patientid = b.patientid..
swolff1978