I assumed that SQL 2008 is not specified.
SELECT X.TimePeriod, Count( CASE X.Which WHEN 3 THEN CASE WHEN App_Date > DateAdd(Day, -DatePart(Weekday, GetDate()), GetDate()) THEN 1 END WHEN 2 THEN CASE WHEN Month(App_Date) = Month(GetDate()) THEN 1 END ELSE 1 END ) [No of Appointments] FROM tblApplication CROSS JOIN ( VALUES (1, 'Year to date'), (2, 'Month to date'), (3, 'Week to date') ) X (Which, TimePeriod) WHERE App_Date < Convert(date, GetDate() + 1) AND App_Date >= DateAdd(Year, DateDiff(Year, '19000101', App_Date), '19000101') GROUP BY X.Which, X.TimePeriod ORDER BY X.Which
If you have a lot of data in your table and an index in App_Date, this query will work much better than one, using date functions on the entire table without filtering.
I also have a built-in assumption that your App_Date values do not have a time part (they are all set to 12am). If this is not the case, please let me know so that I can change case 3 correctly.
If someone wants to try the code here, make the following settings:
CREATE TABLE tblApplication ( App_Date datetime ) INSERT tblApplication VALUES ('2/1/2012'), ('2/5/2012'), ('2/10/2012'), ('1/2/2012'), ('1/9/2012'), ('1/15/2012'), ('1/28/2012'), ('12/1/2012'), ('12/5/2012'), ('12/10/2012'), ('11/2/2012'), ('11/9/2012'), ('11/15/2012'), ('11/28/2012')
Sorry to not use 'YYYYMMDD', I didn’t think when I printed it.