|
|
Question : Grouping with DISTINCT COUNT
|
|
|
|
Hi Guys
I have made a query that can select all the mail received per day per month in a year, and it works as it should. Now i need this to support to count the number of distinct customers that receive mail per day instead of the totalt mail count.
The column is called CustomerId
How would i go by doing that here?
in the bottom you can see a select that does what i want, but i want it in my master select
/*master select*/ SELECT year(t1.CreatedDate) as [Year], month(t1.CreatedDate), Day1 = sum(case when day(t1.CreatedDate) = 1 then 1 else 0 end), Day2 = sum(case when day(t1.CreatedDate) = 2 then 1 else 0 end), Day3 = sum(case when day(t1.CreatedDate) = 3 then 1 else 0 end), Day4 = sum(case when day(t1.CreatedDate) = 4 then 1 else 0 end), Day5 = sum(case when day(t1.CreatedDate) = 5 then 1 else 0 end), Day6 = sum(case when day(t1.CreatedDate) = 6 then 1 else 0 end), Day7 = sum(case when day(t1.CreatedDate) = 7 then 1 else 0 end), Day8 = sum(case when day(t1.CreatedDate) = 8 then 1 else 0 end), Day9 = sum(case when day(t1.CreatedDate) = 9 then 1 else 0 end), Day10 = sum(case when day(t1.CreatedDate) = 10 then 1 else 0 end), Day11 = sum(case when day(t1.CreatedDate) = 11 then 1 else 0 end), Day12 = sum(case when day(t1.CreatedDate) = 12 then 1 else 0 end), Day13 = sum(case when day(t1.CreatedDate) = 13 then 1 else 0 end), Day14 = sum(case when day(t1.CreatedDate) = 14 then 1 else 0 end), Day15 = sum(case when day(t1.CreatedDate) = 15 then 1 else 0 end), Day16 = sum(case when day(t1.CreatedDate) = 16 then 1 else 0 end), Day17 = sum(case when day(t1.CreatedDate) = 17 then 1 else 0 end), Day18 = sum(case when day(t1.CreatedDate) = 18 then 1 else 0 end), Day19 = sum(case when day(t1.CreatedDate) = 19 then 1 else 0 end), Day20 = sum(case when day(t1.CreatedDate) = 20 then 1 else 0 end), Day21 = sum(case when day(t1.CreatedDate) = 21 then 1 else 0 end), Day22 = sum(case when day(t1.CreatedDate) = 22 then 1 else 0 end), Day23 = sum(case when day(t1.CreatedDate) = 23 then 1 else 0 end), Day24 = sum(case when day(t1.CreatedDate) = 24 then 1 else 0 end), Day25 = sum(case when day(t1.CreatedDate) = 25 then 1 else 0 end), Day26 = sum(case when day(t1.CreatedDate) = 26 then 1 else 0 end), Day27 = sum(case when day(t1.CreatedDate) = 27 then 1 else 0 end), Day28 = sum(case when day(t1.CreatedDate) = 28 then 1 else 0 end), Day29 = sum(case when day(t1.CreatedDate) = 29 then 1 else 0 end), Day30 = sum(case when day(t1.CreatedDate) = 30 then 1 else 0 end), Day31 = sum(case when day(t1.CreatedDate) = 31 then 1 else 0 end), COUNT(*) TOTAL FROM MailReceived t1 WHERE Year(t1.CreatedDate) = 2010 GROUP BY year(t1.CreteadDate), month(t1.CreatedDate) order by TOTAL DESC
/*Customers per day*/ SELECT TOP 100 convert(datetime, convert(char(10), t1.CreatedDate, 101)) thedate ,count(DISTINCT(t1.CustomerId)) Total FROM MailReceived t1 WHERE BusinessCenterId=4 GROUP BY convert(datetime, convert(char(10), t1.CreatedDate, 101)) ORDER BY thedate desc
|
|
|
|
Answer : Grouping with DISTINCT COUNT
|
|
|
|
instead of Day1 = sum(case when day(t1.CreatedDate) = 1 then 1 else 0 end), you want Day1 = count(distinct case when day(t1.CreatedDate) = 1 then customerid else null end), etc
|
|
|
|
|