Solved

how to pivot dataset

Posted on 2014-02-21
3
180 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
[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
3 Comments
 
LVL 25

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

738 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