Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am always stuck with Pivoting tables, I feel it is most difficult topic in sql,
Below is the expected result which i want, can somebody help?

Main Table
ProductType     | YTD_SPD     | Var_SPD     | Per_SPD
OutboundNON DOC | 2448.029903 |	244.0843848 | 11.07488288
InboundNON DOC  | 364.819701  | 68.18525457 | 22.98629016


Output Expected
ProductType | OutboundNON DOC | InboundNON DOC
YTD_SPD     | 2448.029903     |	364.819701
Var_SPD     | 244.0843848     | 68.18525457
Per_SPD     | 11.07488288     | 22.98629016


What I have tried:

SELECT 'YTD_SPD' ProductType, 
[OutboundNON DOC], [InboundNON DOC],[InboundDOC],[OutboundDOC]
FROM
(SELECT ProductType,YTD_SPD, Var_SPD from Product_Performance2) AS SourceTable
PIVOT
(
 SUM(Var_SPD)
 FOR ProductType IN ([OutboundNON DOC],[InboundNON DOC],[InboundDOC],[OutboundDOC])
) AS PivotTable;
Posted
Updated 22-Jun-18 2:19am
v2

1 solution

You can't just pivot data, you need to unpivot it first. Check this:

SQL
DECLARE @tmp TABLE (ProductType NVARCHAR(30), YTD_SPD DECIMAL(20,8), Var_SPD DECIMAL(20,8), Per_SPD DECIMAL(20,8))

INSERT INTO @tmp (ProductType, YTD_SPD, Var_SPD, Per_SPD)
VALUES('OutboundNON DOC', 2448.029903, 244.0843848, 11.07488288),
('InboundNON DOC', 364.819701, 68.18525457, 22.98629016)

--SELECT ColName As ProductType, [OutboundNON DOC], [InboundNON DOC]
--FROM (
	SELECT  ColName, ProductType, Data 
	FROM (
		SELECT *
		FROM @tmp
	) AS pvt1
	UNPIVOT(Data FOR ColName IN (YTD_SPD, Var_SPD, Per_SPD)) AS unpvt
--) AS src
--PIVOT(SUM(Data) FOR ProductType IN ([OutboundNON DOC], [InboundNON DOC])) AS pvt2

-- ==================================
-- Above code returns unpivoted form
-- Uncomment lines to see final form
-- ==================================


See MSDN documentation: Using PIVOT and UNPIVOT | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
CHill60 22-Jun-18 8:20am    
Beat me to it. 5'd
Maciej Los 22-Jun-18 8:22am    
Thank you, Caroline.
;)
sunil mali 22-Jun-18 8:28am    
Wow.. Thank you so much, You are a true star, Can you please help me with some link where i can learn pivotes in deep.
Maciej Los 22-Jun-18 8:43am    
You're very welcome.
MSDN documentation is where you can start. Then you need to practice, practice and practice, because practice makes you master. ;)
There's few sites i can recommend you:
mssqltips.com[^]
blog.aqlauthority.com - by Pinal Dave[^]
SQLServerCentral[^]

Good luck!
CHill60 22-Jun-18 9:50am    
If I may, here is a list of CodeProject articles on the subject (although not all of them are just about SQL Pivot) ... Search[^]
I've picked out two that are good starting points...
Simple Way To Use Pivot In SQL Query[^]
Pivot Operator in SQL Server Simplified[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900