Solved

how to pivot dataset

Posted on 2014-02-21
3
178 Views
Last Modified: 2014-03-04
hello,

I have a dataset that looks this way:

current output
I want to be able to display the results this way:

desire output
my output select is:

	SELECT DISTINCT
		[ItemNumber]
	,	[ItemDescription]
	,	[ItemFamilyGroupCode]
	,	[ItemFamilyTypeCode]
	,	[ItemCategoryCode]
	,	[ItemSweetenerGroupCode]
	,	[ItemQuantity]
	,	[SellingPrice]
	,	[MaterialCost]
	,	[LaborCost]
	,	[VariableOverHeadCost]
	,	[ShippingCost]
	,	[OtherCost]
	,	[TotalVariableCost]
	,	[SellingPrice] 
		- [TotalVariableCost] AS [VariableMargin]
	,	[FixedOverHeadCost]
	,	[SGACost]
	,	[TotalVariableCost] 
		+ [FixedOverHeadCost] 
		+ [SGACost] AS [TotalCost]
	,	[NetMargin] = 
					CASE [SellingPrice]
						WHEN 0 THEN 0
						ELSE ([SellingPrice] - [TotalCost]) / [SellingPrice]
					END
	,	[PostingDate]					
	FROM 
		@VariousMarginsTable 
	ORDER BY 
		ItemNumber ASC
	,	PostingDate DESC

Open in new window


how can i obtain the desire out put that is the pivoted output?

any help would be great.

thank a lot!
0
Comment
Question by:metropia
3 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39881410
I do not think you need to PIVOT the data. It looks like that you are trying to UNION different groups of data. I think a simple query as this will achieve what you are after:
;with FGC AS
(
SELECT CASE WHEN GROUPING([ItemFamilyGroupCode])=1 THEN 12 ELSE 11 END AS orderColumn,
CASE WHEN GROUPING([ItemFamilyGroupCode])=1 THEN 'Total' ELSE [ItemFamilyGroupCode] END AS GroupType, 
SUM([ItemQuantity]) AS Quantity
FROM VariousMarginsTable
GROUP BY [ItemFamilyGroupCode] WITH ROLLUP
),
 FTC AS
(
SELECT CASE WHEN GROUPING([ItemFamilyTypeCode])=1 THEN 22 ELSE 21 END AS orderColumn,
CASE WHEN GROUPING([ItemFamilyTypeCode])=1 THEN 'Total' ELSE [ItemFamilyTypeCode] END AS GroupType, 
SUM([ItemQuantity]) AS Quantity
FROM VariousMarginsTable
GROUP BY [ItemFamilyTypeCode] WITH ROLLUP
),
 CC AS
(
SELECT CASE WHEN GROUPING([ItemCategoryCode])=1 THEN 32 ELSE 31 END AS orderColumn,
CASE WHEN GROUPING([ItemCategoryCode])=1 THEN 'Total' ELSE [ItemCategoryCode] END AS GroupType, 
SUM([ItemQuantity]) AS Quantity
FROM VariousMarginsTable
GROUP BY [ItemCategoryCode] WITH ROLLUP
),
 SGC AS
(
SELECT CASE WHEN GROUPING([ItemSweetenerGroupCode])=1 THEN 42 ELSE 41 END AS orderColumn,
CASE WHEN GROUPING([ItemSweetenerGroupCode])=1 THEN 'Total' ELSE [ItemSweetenerGroupCode] END AS GroupType, 
SUM([ItemQuantity]) AS Quantity
FROM VariousMarginsTable
GROUP BY [ItemSweetenerGroupCode] WITH ROLLUP
)
SELECT GroupType, Quantity 
FROM 
(
SELECT 10 as orderColumn,
'Family Group' AS GroupType,
'Quantity' AS Quantity
  UNION ALL
SELECT 20 as orderColumn,
'Family Type' AS GroupType,
'Quantity' AS Quantity
  UNION ALL
SELECT 30 as orderColumn,
'Item Category Code' AS GroupType,
'Quantity' AS Quantity
  UNION ALL
SELECT 40 as orderColumn,
'Sweetener Type' AS GroupType,
'Quantity' AS Quantity
  UNION ALL
SELECT  orderColumn, GroupType, CONVERT(VARCHAR(MAX), Quantity) AS Quantity FROM FGC
  UNION ALL
SELECT  orderColumn, GroupType, CONVERT(VARCHAR(MAX), Quantity) AS Quantity  FROM FTC
  UNION ALL
SELECT  orderColumn, GroupType, CONVERT(VARCHAR(MAX), Quantity) AS Quantity  FROM CC
  UNION ALL
SELECT  orderColumn, GroupType, CONVERT(VARCHAR(MAX), Quantity) AS Quantity  FROM SGC
) AS a ORDER BY orderColumn

Open in new window

SQL Fiddle
0
 

Author Closing Comment

by:metropia
ID: 39904574
Thank you! and sorry for the delay i was pulled to a different task and only now was able to try your solution. it helped me a lot to figure out what I needed to do.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 the fundamental information of how to create a table.

840 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