SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamic Pivot for multiple columns


Dynamic Pivot for multiple columns

Author
Message
gksharmaajmer
gksharmaajmer
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 Visits: 112
Hi,
Here is my friend's sql server version info :

Microsoft SQL Server 2000 - 8.00.2039 (Intel X86) May 3 2005 23:18:38 Copyright (c) 1988-2003 Microsoft Corporation Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)

Table structure and sample insert statements :

create table consumption (
code varchar(6),
prodname varchar(50),
department varchar(20),
netqty decimal(10,2),
netmrp decimal(10,2)
)

insert into consumption values ('U00180','USG JELLY IM-K','SONO',11.4,130.40)
insert into consumption values ('U00280','UNIDINE 1 LITRE SOLUTION','AKD',1.4,10.40)
insert into consumption values ('V02401','VOLINI GEL 50GM','PHYSIOTHERAPY',8,15)
insert into consumption values ('V02402','X RAY FILM DIGITAL 14"X 17"','MRI',3,26.40)
insert into consumption values ('U00380','TROPAC P DROPS ','AKD',21.46,56.78)
insert into consumption values ('U00380','TROPAC P DROPS ','AKD',10,10)

Table Data :


code prodname department netqty netmrp
U00180 USG JELLY IM-K SONO 11.40 130.40
U00280 UNIDINE 1 LITRE SOLUTION AKD 1.40 10.40
V02401 VOLINI GEL 50GM PHYSIOTHERAPY 8.00 15.00
V02402 X RAY FILM DIGITAL 14"X 17" MRI 3.00 26.40
U00380 TROPAC P DROPS AKD 21.46 56.78
U00380 TROPAC P DROPS AKD 10.00 10.00



Required Output please :


CODE PRODNAME AKD MRI PHYSIOTHERAPY SONO
NET QTY NET MRP NET QTY NET MRP NET QTY NET MRP NET QTY NET MRP
U00180 USG JELLY IM-K 11.40 130.40
U00280 UNIDINE 1 LITRE SOLUTION 1.40 10.40
U00380 TROPAC P DROPS 31.46 66.78
V02401 VOLINI GEL 50GM 8.00 15.00
V02402 X RAY FILM DIGITAL 14"X 17" 3.00 26.40



I have google and implemented one column pivot i.e. NET QTY column by the help of below link :
http://www.sqlteam.com/article/dynamic-cross-tabs-pivot-tables

But here he needs to pivot more than one column with dynamic pivoting. I am sure, it has been asked many times, if you please share any link and/or solution, we shall be thankful to you.

Kindly let me know, if I am unclear in my question and/or needs to provide more info.

Thanks and Regards
Girish Sharma
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16709 Visits: 19108
There are 2 great articles from Jeff Moden. Here's the part on dynamic pivoting
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
gksharmaajmer
gksharmaajmer
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 Visits: 112
I solved the question with the help of below link :
http://stackoverflow.com/questions/14694691/sql-server-pivot-table-with-multiple-column-aggregates


declare @Sql nvarchar(max)
select @Sql='SELECT min(t.code) As CODE,PRODNAME'
DECLARE @ColumnTemplate NVARCHAR(MAX)
select @ColumnTemplate='SUM(CASE WHEN t.department=''{department}'' THEN t.netqty ELSE 0 END) AS "{department}_NetQty"
,SUM(CASE WHEN t.department=''{department}'' THEN t.netmrp ELSE 0 END) AS "{department}_NetMrp"'
SELECT @Sql=@Sql+CHAR(13)+','+REPLACE(@ColumnTemplate, '{department}', REPLACE(c.name,'''',''''''))
FROM (
SELECT DISTINCT t.department AS name
FROM consumption t
) c
SELECT @Sql=@Sql+'FROM consumption t GROUP BY t.prodname order by code;'
--PRINT @Sql;
EXEC(@Sql);



Regards
Girish Sharma
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search