Need Query for getting MONTHNAME,YEAR from my DateColumn SQLSERVER
-
Hi, I have a column named posted date, Depending on the above column I need to count No of records per year and month wise asc. I done by using group by function but am not able to get jan --- desc order. Result ie:MonthName,Year,Count columns
-
Hi, I have a column named posted date, Depending on the above column I need to count No of records per year and month wise asc. I done by using group by function but am not able to get jan --- desc order. Result ie:MonthName,Year,Count columns
Hi, I have below records in my table 'Sales' ID SalesName LastUpdated ------------------------------------------------- 1 S1 2009-07-16 13:27:23.890 2 S2 2009-08-16 13:32:58.127 3 S3 2009-08-16 13:33:01.987 4 S4 2009-08-16 13:34:21.733 5 S5 2009-09-16 13:32:40.703 6 S6 2011-01-16 13:32:23.703 7 S7 2011-02-16 13:32:23.703 8 S8 2011-02-16 13:32:23.703 9 S9 2011-03-16 13:32:23.703 10 S10 2011-07-16 13:32:23.703 -------------------------------------------------- Below query gives me the count of sales for different months on yearly basis. SELECT DISTINCT MonthCount = (SELECT COUNT(DATEPART("mm",LastUpdated)) FROM Sales WHERE DATEPART("yy",LastUpdated) = DATEPART("yy",p.LastUpdated) AND DATEPART("mm",LastUpdated) = DATEPART("mm",p.LastUpdated)) , "Month" = DATENAME(Month,LastUpdated),"Year"=DATEPART("yy",p.LastUpdated) FROM Sales p ORDER BY DATEPART("yy",p.LastUpdated) Here is my desired result: MonthCount Month Year ----------------------------- 1 July 2009 1 September 2009 3 August 2009 1 January 2011 1 July 2011 1 March 2011 2 February 2011