Fairly new to SQL and need a quick answer. I have been looking for most of today and need a quick answer so I apologize if this is fairly basic. I am looking for a way to 'summarize' the columns from this query.
SELECT
Count(case when [BatchNumber] < '100' then 1 else 0 end) as A,
Count(case when [BatchNumber] like '22%' then 1 else 0 end) as B,
Count(case when [BatchNumber] like '33%' then 1 else 0 end) as C,
FROM [Database].[dbo].[Transaction]
WHERE [Date] between '2012-01-03' and '2012-01-05'
Group by [Date]
Data in the column is similar to:
Date Batchnumber
2012-01-03 1
2012-01-03 2
2012-01-03 3
2012-01-03 4
2012-01-03 2201
2012-01-03 2202
2012-01-03 3301
2012-01-03 3302
2012-01-03 3303
2012-01-05 1
2012-01-05 2
2012-01-05 3
2012-01-05 4
2012-01-05 5
2012-01-05 3301
2012-01-05 3302
2012-01-05 3303
2012-01-05 3304
Looking for output something a long the lines of:
Date A B c
2012-01-03 4 2 3
2012-01-05 5 0 4
Thank you in advance for any help.
EDIT: Rolling this back as I did not present my initial problem correctly. The info provided does answer the initial post.
COUNT(CASE WHEN...?'12-01-05'to'2012-01-05'in order to be sure it gets interpreted correctly.COUNTdoesn't lack theGROUP BYanymore, but has the problem that both 0 and 1 are not null, so both get counted.COUNT(CASE WHEN ... THEN 1 ELSE 0 END)is hence the same asCOUNT(*). You wantSUM(CASE WHEN ... THEN 1 ELSE 0 END)instead orCOUNT(CASE WHEN ... THEN 1 ELSE NULL END)or shortCOUNT(CASE WHEN ... THEN 1 END).