Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to pivot dataset

Posted on 2014-02-21
3
Medium Priority
?
183 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 2000 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

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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 how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

721 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