My database table Holiday includes field ad Holidaydate->datetime , WeekendHoliday->bit, PublicHoliday->bit I want the nos of weekendholiday and also publicholiday in a specific month year.
My table values are as(example)
Holidaydate WeekendHoliday PublicHoliday
----------- -------------- -------------
4/02/2012 true false
4/20/2012 true false
5/3/2012 true False
5/30/2012 false true
4/05/2013 false true
So now output should be like this:
year Month count(weekend) count(public)
---- ----- -------------- -------------
2012 April 2 0
2012 May 1 1
2013 April 0 1
I strongly suggest to use a calendar table for such kind of queries.
Here's one way how you could aggregate your data:
The
partitioned
CTE replaces every date with the first of the same month, and that value is then used for grouping and deriving years and month names from. EachNULLIF()
transforms every'false'
(0
) value intoNULL
so the correspondingCOUNT()
omits it. As a result, only the'true'
(1
) values are counted.I don't have SQL server available. THis is tested on mysql. Here year and month are function to return the year and month of date. Syntax of CASE should be same across database.