CASE WHEN COUNT and GROUP BY

Greetings...I have the following statement that produces the expected results of id, Name and Points. However, I would like to 'sum' the count where names are duplicates. In other words, if a name has two entries (each having Points=1) I would like one entry where Points=2.

If I uncomment my GROUP BY I get only one name, but the Points value remains 1. What am I doing wrong? Thanks...Scott

SELECT b.[id],
            b.[Name],
            CASE WHEN ((TrackingDateTime) IS NOT NULL AND (ReceivedByHESDateTime) IS NOT NULL) THEN 1 ELSE 0 END AS Points
      FROM DocumentTracking a
      INNER JOIN companies b ON (a.companyid = b.[id])
      WHERE TrackingDateTime >= '10/28/03'
            AND TrackingDateTime <= '10/28/04'
      --GROUP BY b.[id], b.[name]
      ORDER BY b.[name]
rsanglimAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HilaireCommented:
try

SELECT b.[id],
          b.[Name],
          SUM(CASE WHEN ((TrackingDateTime) IS NOT NULL AND (ReceivedByHESDateTime) IS NOT NULL) THEN 1 ELSE 0 END) AS Points
     FROM DocumentTracking a
     INNER JOIN companies b ON (a.companyid = b.[id])
     WHERE TrackingDateTime >= '10/28/03'
          AND TrackingDateTime <= '10/28/04'
     GROUP BY b.[id], b.[name]
     ORDER BY b.[name]
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HilaireCommented:
I just added a SUM group function to collapse the records that belong to the same group into one single record
0
Julian HansenCommented:
I think Hilaire has nailed it.

The GROUP BY works with the agregate functions SUM, MAX, MIN etc without specifying an agregate function SQL does not know what you want to do - i.e. it cannot know that you want to sum the points - you might want to find the MAX or the MIN.


0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

rsanglimAuthor Commented:
Thanks, Hilaire...that was it! I appreciate the quick reply :)
0
rsanglimAuthor Commented:
...and thanks julian for the explanation!
0
Julian HansenCommented:
;) you are most welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.