group by, distinct not working as expected

  • Hi,

    I have this sql....

    SELECT

    dbPatID, dbAddDate, dbStaffLastName, RefTypeWord

    FROM

    EPSReferralKPIs

    WHERE

    (dbAddDate >= '2013-01-01' OR '2013-01-01' = '')

    AND (dbAddDate <= '2013-12-31' OR '2013-12-31' = '')

    AND (dbStaffLastName IN ('Swanepoel','Patient','Pelletti','Ray','Qureshi','Grobler','Hedborg','De Kock','Lima','Check In','Hodgson')

    AND (RefTypeWord IN ('PATIENT','OTHER','DOCTOR','','SIBLING')

    ORDER BY

    dbAddDate

    There may be more than one RefTypeWord for a dbPatId however I only want the result set to bring back unique dbPatID's and not more than one row containing a different RefTypeWord per row.

    Is that possible?

    thanks,

  • What about the other columns? Can you show a few rows of what you have and what you would like to see? Remember - we can't see what you can, we're relying upon your description.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • The current sql will bring back

    900032592013-05-28 16:05:13.000GroblerDOCTOR

    900032592013-05-28 16:05:13.000GroblerOTHER

    Where as I want it to bring back just one row for each dbPatID, I'm not bothered which RefTypeWord it is

    thanks

  • Try this as a test:

    SELECT

    dbPatID,

    dbAddDate,

    dbStaffLastName,

    RefTypeWord = MAX(RefTypeWord)

    FROM EPSReferralKPIs

    WHERE

    dbAddDate >= '2013-01-01' OR '2013-01-01' = ''

    AND dbAddDate <= '2013-12-31' OR '2013-12-31' = ''

    AND dbStaffLastName IN ('Swanepoel','Patient','Pelletti','Ray','Qureshi','Grobler','Hedborg','De Kock','Lima','Check In','Hodgson')

    AND RefTypeWord IN ('PATIENT','OTHER','DOCTOR','','SIBLING')

    GROUP BY dbPatID, dbAddDate, dbStaffLastName

    ORDER BY dbAddDate

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Thanks, I've actually worked out we don't need to select the RefTypeWord, so removing this from the select and making dbPatID distinct bought back the correct amount of rows.

    I compared this with your sql and it brings the same result set back which is good.

    thanks for your help.

  • Just a thought...what's this meant to do?

    (dbAddDate >= '2013-01-01' OR '2013-01-01' = '')

    AND (dbAddDate <= '2013-12-31' OR '2013-12-31' = '')

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 6 posts - 1 through 5 (of 5 total)

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