Question: SQL Query

Back in July 2010, I was asked the following question by Pramod:

Is it possible to get a Pivot Table kind of an output as a result of a query. For example I have a table like this

Id Date Sales Region

and as an output I want the a list of region wise sales with a monthly breakup and the respective sales.

Can you help?

My suggestion was to use the query below:

SELECT
	Region, [1] AS Jan2010, [2] AS Feb2010, [3] AS Mar2010,
	[4] AS Apr2010, [5] AS May2010, [6] AS June2010, [7] AS July2010
FROM
(SELECT Region, Sales, MONTH([Date]) AS SalesMonth
FROM Sales) S
PIVOT
(
SUM (Sales)
FOR SalesMonth IN
( [1], [2], [3], [4], [5], [6], [7] )
) AS pvt
ORDER BY pvt.Region

This assumed at the time that all the data was for 2010.

Updated: Wednesday, 16th September, 2015 — 2:18 pm

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.