1

I have a table that contains datefield and product. I want the below pivot query to show the month total, quarter total and year total.

Year Month A B C
-----------------
2014 Jan   
2014 Feb
2014 Mar 
2014 Q1
2014 Apr
2014 May
2014 Jun
2014 Q2
2014 Jul
2014 Aug
2014 Sep
2014 Q3
2014 Oct
2014 Nov
2014 Dec
2014 Q4
2014 Total  
2015 Jan
2015 Feb
2015 Mar
2015 Q1 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
fred
  • 19
  • 1
  • possible duplicate of [SQL Server PIVOT examples?](http://stackoverflow.com/questions/24470/sql-server-pivot-examples) – shA.t Mar 17 '15 at 04:11
  • Total of what? What are A, B, C? – Giorgi Nakeuri Mar 17 '15 at 07:47
  • The table contains the enrollment detail, product: Course A,Course B, Course C, i would like to show the total count of different course by month, quarter and year in one query, hope someone can help – fred Mar 17 '15 at 08:49

2 Answers2

0

Refer this Simple-Way-To-Use-Pivot-In-SQL-Query from CodeProject, the code given in the example is more similar to your requirement.

SelvaS
  • 2,105
  • 1
  • 22
  • 31
0

I think this is what you are talking about.

SELECT 'Count' AS Month, 
[Jan], [Feb]
FROM
(SELECT year, month 
    FROM Product) AS SourceTable
PIVOT
(
count(year)
FOR Month IN ([Jan],[Feb])
) AS PivotTable;

Out Put

Month   Jan Feb
Count   2   2

This is what you are expecting.

Count   Month   quarter Year
Count   24       8        2

But We cannot categorise month, year unless they are designated as months, quarter etc. If there is any flag which denotes that Jan and Feb are months then we can categorise them based on Months.