COMPLEX DISTINCT QUERY

  • I am working a hospital patient event data set. Currently my extract list all patient activity within each care specilaty. Consequently some patients have more than more than one record per care specialty. However I am required to reduce the extract such that it shows distinct patients for each care specialty. Any ideas on how I can do this.

    Thanks

  • select Distinct

    or use Group by and Aggregate functions on the required columns.

    if you want specific help then post the query and table structure..

  • the current query is

    SELECT AR.SequenceID,CL.ClientID,GPP.PCG AS PrimaryTrust,AAP.AppointmentDate,APT.CodeDescription AS AppointmentType,ARS.CodeDescription AS ReferralSource,

    GS.CodeDescription AS Specialty,convert(varchar(10),CL.DateOfBirth,110) as [Date Of Birth],CL.NNN AS NHS_NUMBER,GE.CodeDescription as Ethnicity, CGP.GPCode,CA.PostCode,WR.WARD_ID,WR.WARD_NAME

    FROM AMSAppointment AAP

    INNER JOIN AMSAppointmentContact APC ON APC.SequenceID = AAP.SequenceID

    INNER JOIN AMSreferral AR ON AR.ClientID = APC.ClientID AND AR.ReferralNumber = APC.ReferralID

    LEFT OUTER JOIN GenSpecialty GS

    ON GS.Code = AR.SpecialtyReferredTo

    LEFT OUTER JOIN AmsReferralSource ARS

    ON ARS.Code = AR.ReferralSource

    LEFT OUTER JOIN CLIENT CL

    ON CL.ClientID = APC.ClientID

    LEFT OUTER JOIN GenEthnicity GE

    ON CL.Ethnicity = GE.Code

    LEFT OUTER JOIN ClientGP CGP

    ON CGP.ClientID = APC.ClientID AND CGP.ToDate IS NULL

    LEFT OUTER JOIN ClientAddress CA

    ON CA.ClientID = APC.ClientID AND CA.ToDate IS NULL

    LEFT OUTER JOIN AmsAppointmentType APT

    ON APT.Code = AAP.AppointmentType

    INNER JOIN GenGP GP ON CGP.GPCode = GP.CODE --GET GP description

    INNER JOIN GenGPPractice GPP ON CGP.PracticeCode = GPP.Code --GET PRACTISE CODE DETAILS

    LEFT OUTER JOIN BRENTREPODB02.Olympus.dbo.POSTCODE BR ON BR.POST_CODE = CA.PostCode

    LEFT OUTER JOIN BRENTREPODB02.Olympus.dbo.WARD WR ON WR.WARD_ID = BR.WARD_ID

    WHERE AAP.AppointmentDate >= '2008/10/01' AND AAP.AppointmentDate <= '2008/12/31'

    Order by GS.CodeDescription

  • I forgot to add.

    CL.ClientID is the patient ID and

    GS.CodeDescription AS Specialty is the care specialty field

  • So you want to group by the ClientId and CodeDescription

    Out of the other columns, which ones have values that are not distinct?

    Or post a sample of the output of the query..

  • I have attached a screenshot of the output.

  • SELECT

    CL.ClientID,

    GS.CodeDescription AS Specialty

    FROM

    AMSAppointment AAP INNER JOIN AMSAppointmentContact APC

    ON APC.SequenceID = AAP.SequenceID INNER JOIN AMSreferral AR

    ON AR.ClientID = APC.ClientID AND AR.ReferralNumber = APC.ReferralID LEFT OUTER JOIN GenSpecialty GS

    ON GS.Code = AR.SpecialtyReferredTo LEFT OUTER JOIN AmsReferralSource ARS

    ON ARS.Code = AR.ReferralSource LEFT OUTER JOIN CLIENT CL

    ON CL.ClientID = APC.ClientID LEFT OUTER JOIN GenEthnicity GE

    ON CL.Ethnicity = GE.Code LEFT OUTER JOIN ClientGP CGP

    ON CGP.ClientID = APC.ClientID AND CGP.ToDate IS NULL LEFT OUTER JOIN ClientAddress CA

    ON CA.ClientID = APC.ClientID AND CA.ToDate IS NULL LEFT OUTER JOIN AmsAppointmentType APT

    ON APT.Code = AAP.AppointmentType INNER JOIN GenGP GP

    ON CGP.GPCode = GP.CODE -- GET GP description

    INNER JOIN GenGPPractice GPP

    ON CGP.PracticeCode = GPP.Code --GET PRACTISE CODE DETAILS

    LEFT OUTER JOIN BRENTREPODB02.Olympus.dbo.POSTCODE BR

    ON BR.POST_CODE = CA.PostCode LEFT OUTER JOIN BRENTREPODB02.Olympus.dbo.WARD WR

    ON WR.WARD_ID = BR.WARD_ID

    WHERE

    AAP.AppointmentDate >= '2008/10/01' AND AAP.AppointmentDate <= '2008/12/31'

    GROUP BY

    CL.ClientID,

    GS.CodeDescription

    Order by

    GS.CodeDescription

  • Many thanks.

  • just one more thing. How can I alter your script so that I can bring in all the other fields that were in the original extract?

  • For the values that are repeating over the rows, such as referral source and speciality then you just add these to the select statement and group by clause.

    But for the other values you need to work out what you want to do with them, for example what appoinment date do you want ? min / max

  • I need the max appointment date

  • okay then you add this with a aggregate funciton in the select clause

    SELECT

    CL.ClientID,

    GS.CodeDescription AS Specialty,

    MAX(AAP.AppointmentDate) as MaxAppointmentDate

    FROM

    AMSAppointment AAP INNER JOIN AMSAppointmentContact APC

    ON APC.SequenceID = AAP.SequenceID INNER JOIN AMSreferral AR

    ON AR.ClientID = APC.ClientID AND AR.ReferralNumber = APC.ReferralID LEFT OUTER JOIN GenSpecialty GS

    ON GS.Code = AR.SpecialtyReferredTo LEFT OUTER JOIN AmsReferralSource ARS

    ON ARS.Code = AR.ReferralSource LEFT OUTER JOIN CLIENT CL

    ON CL.ClientID = APC.ClientID LEFT OUTER JOIN GenEthnicity GE

    ON CL.Ethnicity = GE.Code LEFT OUTER JOIN ClientGP CGP

    ON CGP.ClientID = APC.ClientID AND CGP.ToDate IS NULL LEFT OUTER JOIN ClientAddress CA

    ON CA.ClientID = APC.ClientID AND CA.ToDate IS NULL LEFT OUTER JOIN AmsAppointmentType APT

    ON APT.Code = AAP.AppointmentType INNER JOIN GenGP GP

    ON CGP.GPCode = GP.CODE -- GET GP description

    INNER JOIN GenGPPractice GPP

    ON CGP.PracticeCode = GPP.Code --GET PRACTISE CODE DETAILS

    LEFT OUTER JOIN BRENTREPODB02.Olympus.dbo.POSTCODE BR

    ON BR.POST_CODE = CA.PostCode LEFT OUTER JOIN BRENTREPODB02.Olympus.dbo.WARD WR

    ON WR.WARD_ID = BR.WARD_ID

    WHERE

    AAP.AppointmentDate >= '2008/10/01' AND AAP.AppointmentDate <= '2008/12/31'

    GROUP BY

    CL.ClientID,

    GS.CodeDescription

    Order by

    GS.CodeDescription

    If that is all the columns you need then this query should be fine. Otherwise you need to work out what aggregate functions to perform on the remainder of columns

  • Thank you very much. You have been a great help

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply