Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2003
    Posts
    29

    Unanswered: question regarding CASE and GROUP BY

    I have the below query and I am not sure if this will return an accurate aggregate, I know I cannot just group by my alias GLG_DELEGATE_ID, is this the way to handle aggregates when you have a CASE in the SELECT statement?

    SELECT CASE
    WHEN C.GLG_DELEGATE_ID IS null THEN C.GLG_ID
    ELSE C.GLG_DELEGATE_ID
    END AS GLG_DELEGATE_ID
    ,COUNT(P.CONSULTATION_ID) ACTIVITY_AMOUNT
    FROM
    dbo.CONSULTATION C
    GROUP BY
    C.GLG_DELEGATE_ID
    , C.GLG_ID

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    No, just repeat your CASE code in the WHERE clause:

    SELECT CASE WHEN C.GLG_DELEGATE_ID IS null THEN C.GLG_ID
    ELSE C.GLG_DELEGATE_ID
    END AS GLG_DELEGATE_ID
    ,COUNT(P.CONSULTATION_ID) ACTIVITY_AMOUNT
    FROM dbo.CONSULTATION C
    GROUP BY CASE WHEN C.GLG_DELEGATE_ID IS null THEN C.GLG_ID
    ELSE C.GLG_DELEGATE_ID END
    ,C.GLG_ID
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •