Shawn Weisfeld

I find when I talk to myself nobody listens. - Shawn Weisfeld
posts - 352, comments - 144, trackbacks - 34

My Links

News

The views expressed in this blog are mine and mine alone, not that of my employer, Microsoft, or anyone else’s. No warrantee is given for the quality of any material on this site.

Archives

Post Categories

Pivot/Cross Tab Queries in SQL Server V2

Back in 2008 I blogged about Pivot/Cross Tab Queries in SQL Server (http://drowningintechnicaldebt.com/blogs/shawnweisfeld/archive/2008/01/21/pivot-cross-tab-queries-in-sql-server.aspx). I was asked for another example so here goes:

--Sample Sales Table
CREATE TABLE [dbo].[Sales](
    [ClientID] [int] NOT NULL,
    [SalesDate] [datetime] NOT NULL,
    [SalesAmount] [decimal](18, 0) NOT NULL
) ON [PRIMARY]

--Added sample data
SELECT [ClientID]
      ,[SalesDate]
      ,[SalesAmount]
  FROM [Sales]
ClientID SalesDate SalesAmount
1 1/1/2006 100
1 1/1/2007 200
1 1/1/2008 300
1 1/1/2009 400
2 1/1/2006 150
2 1/1/2007 250
2 1/1/2008 250
2 1/1/2009 250
--Create view to report Sales by Year by Client
SELECT [ClientID]
      ,DATEPART(yy, [SalesDate]) AS SalesYear
      ,SUM([SalesAmount]) AS [SalesAmount]
  FROM [Sales]
GROUP BY [ClientID]
      ,DATEPART(yy, [SalesDate])
ClientID SalesYear SalesAmount
1 2006 100
2 2006 150
1 2007 200
2 2007 250
1 2008 300
2 2008 250
1 2009 400
2 2009 250
--for sql server 2005 +
--Pivot to report sales by client over years
SELECT [ClientID], [2006] AS year2006, [2007] AS year2007, [2008] AS year2008, [2009] AS year2009
FROM 
(
 SELECT [ClientID], DATEPART(yy, [SalesDate]) AS SalesYear, [SalesAmount]
 FROM [Sales]) N
PIVOT
(
SUM ([SalesAmount])
FOR SalesYear IN
( 
 [2006], [2007], [2008], [2009] )
) AS pvt
ORDER BY [ClientID]
ClientID year2006 year2007 year2008 year2009
1 100 200 300 400
2 150 250 250 250

Print | posted on Tuesday, March 10, 2009 12:32 AM | Filed Under [ SQL ]

Feedback

Gravatar

# Cross Tab Queries in SQL Server V3

3/10/2009 2:07 AM | drowningintechnicaldebt.com
Gravatar

# re: Pivot/Cross Tab Queries in SQL Server V2

i reallly want your help in makin a cross tab query ... here is the

scnerio . plzzz help me ....


there are many tables .

tb

datatype

sno autogenerate , date varchar(16) , time varchar(10) ,rhrs int,

sno date time rhr_s

1 26/01/2012 07.00 2801

1 26/01/2012 08.00 2802

1 27/01/2012 09.00 2803

1 27/01/2012 10.00 2804


tb1

datatype

sno autogenerate , date varchar(16) , time varchar(10) ,rhr_s1 int,

sno date time rhr_s

1 26/01/2012 07.00 2801

2 26/01/2012 08.00 2802

3 26/01/2012 09.00 2803

4 26/01/2012 10.00 2804

5 27/01/2012 07.00 2811

6 27/01/2012 08.00 2812

7 28/01/2012 09.00 2813

8 28/01/2012 10.00 2814



tb2

datatype

sno autogenerate , date varchar(16) , time varchar(10) ,rhr_s2 int,

sno date time rhr_s2

1 26/01/2012 07.00 2811

2 26/01/2012 08.00 2812

3 27/01/2012 09.00 2813

4 27/01/2012 10.00 2814



i want a crross tab in this way in date format randomly no fixed date


**cols --------------26/01/2012----------------------------27/01/2012

------------------ 28/01/2012**
\
\
\
\
rhr_s--(max-min where date = 26/01/2012) (max-min where date =

27/01/2012) (same formula)
\
\
\
\


rhr_s1 as above as above

as above
\
\
\
\


rhr_s2 as above as above

as above
please help me in making this type of report .....
i reallly i trouble
3/5/2012 4:51 AM | anshu
Gravatar

# re: Pivot/Cross Tab Queries in SQL Server V2

anshu, If the number of date columns can change that none of the standard pivot sql queries will work for you, as they require you to know the number of columns that you are going to have in the result set (as do most reporting tools). If you know you are always going to return less than N columns you might want to create your result set for that many columns. For example if each date in your report represents the week end date for a year to date report you know there are never going to be more than 52 weeks in a year, so you could return all 52 columns and just leave some of them blank. Then in your reporting tool hide the blank columns. Hope that helps point you in the right direction. . .
3/5/2012 9:07 AM | shawnweisfeld
Gravatar

# re: Pivot/Cross Tab Queries in SQL Server V2

Sir i have my columns horizontally but i want date and below that result in like folowwing
Date for 6 days should be there


names ------ 27/01/2012 ------ 28/01/2012 -------29/01/2012------ 30/2012 ------ 31/01/2012

Inst 1 23.00 44.00 55.00 33.00 41.00

inst2 65.00 88.00 99.00 66.00 81.00

inst3 65.00 88.00 99.00 10.00 11.00


i want report in this form data under dates are from different tables as rows are fixed.

3/20/2012 5:01 AM | Anshu
Gravatar

# re: Pivot/Cross Tab Queries in SQL Server V2

Anshu, if you always want 6 days, that actually makes your life much easier. I would name the columns, Day1, Day2, Day3, etc. Then in the query you can do the math to pick the right columns, and in the report you can relabel the columns to the proper dates.
3/20/2012 6:24 AM | shawnweisfeld

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 3 and type the answer here:

Powered by: