Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Dynamic Pivot Table in SQL 2008

Posted on 2010-09-15
3
Medium Priority
?
976 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 93

Accepted Solution

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

Assisted Solution

by:ralmada
ralmada earned 1332 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 1332 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

647 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