Solved

Dynamic Pivot Table in SQL 2008

Posted on 2010-09-15
3
941 Views
Last Modified: 2012-06-21
Experts,

What is the basic SYNTAX to use for a DYNAMIC Pivot Table in SQL Server 2008?

My table looks like this:

HOSP_NAME, SUPPLIER, TOTAL_SPEND, CROSS_REF_SPEND, NOT_CROSS_REF_SPEND

I need to have a pivot like this:

 ------------------------------------ SUPPLIER_1_____SUPPLIER_2____SUPPLIER_3
HOSP_NAME_1                             10000.00                1000.00                 10.00
HOSP_NAME_2                             10000.00                1000.00                 10.00
HOSP_NAME_3                             10000.00                1000.00                 10.00
               

I need the SUPPLIER column heading to be the actual NAMES of the SUPPLIERS
0
Comment
Question by:MIKE
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 167 total points
ID: 33684226
0
 
LVL 41

Assisted Solution

by:ralmada
ralmada earned 333 total points
ID: 33684382
you didn't specify from what column the figures are coming from, so I've assumed they are comming from Total_spend column
basically you do it like this:

DECLARE @strSQL VARCHAR(MAX) 

DECLARE @COLS NVARCHAR(2000) 

  

SELECT @COLS = STUFF((SELECT  distinct '], [' + Supplier  

                                FROM yourtable ORDER BY 1

                                FOR XML PATH('') 

                                ), 1, 2, '') + ']' 

  

  

set @strSQL = 'select Hosp_name, ' + @COLS + ' from 

                (select Hosp_name, Supplier, Total_spend from yourtable) o 

                pivot(max(Total_spend) for Supplier in (' + @cols + '))p' --change max to sum if that's your requirement

  

exec(@strSQL)

Open in new window

0
 
LVL 41

Assisted Solution

by:ralmada
ralmada earned 333 total points
ID: 33684387
oops, typo
DECLARE @strSQL VARCHAR(MAX) 

DECLARE @COLS NVARCHAR(2000) 

  

SELECT @COLS = STUFF((SELECT  distinct '], [' + Supplier  

                                FROM yourtable ORDER BY 1

                                FOR XML PATH('') 

                                ), 1, 2, '') + ']' 

  

  

set @strSQL = 'select Hosp_name, ' + @COLS + ' from 

                (select Hosp_name, Supplier, Total_spend from yourtable) o 

                pivot(max(Total_spend) for Supplier in (' + @cols + ')) as p' --change max to sum if that's your requirement

  

exec(@strSQL)

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now