|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Group by
I want to change the following query so that a Sub_Id will be counted in a particular category ONLY if it is the last Sub_ID entered during the given time period. I believe I need a Select Top and/or Group by DESC subquery, but I'm unfamiliar.
SELECT 'Misc Calls' AS Heading, CASE Sub_Id WHEN 202142 THEN 'Incomplete Info' WHEN 202373 THEN 'General Info' WHEN 201933 THEN 'Workshop' WHEN 202366 THEN 'No Longer Interested' WHEN 202367 THEN 'Unable to Reach' END AS Category, COUNT(DISTINCT c.Business_Id) AS CategoryCount FROM Contacts c INNER JOIN Businesses b ON b.Business_Id = c.Business_Id WHERE YEAR(b.OpenedDate) = 2007 AND MONTH(b.OpenedDate) = 1 AND (Sub_Id = 202142 OR Sub_Id = 202373 OR Sub_Id = 201933 OR Sub_Id = 202366 OR Sub_Id =202367) |
![]() |
| Viewing: Dev Articles Community Forums > Databases > Microsoft SQL Server > Group by |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|