[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dynamic Pivot Table in SQL 2008

Posted on 2010-09-15
3
Medium Priority
?
978 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 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

834 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