SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Pivot Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

brendanb
Starting Member

United Kingdom
7 Posts

Posted - 08/13/2013 :  03:05:50  Show Profile  Reply with Quote
Hi,

I Just need some help with a simple pivot table

I have the following data First Part is the Source

Item Group Price
-------+-----+-------------+
0000001 102 164.900000
0000001 103 164.900000
0000001 106 161.800000
0000003 100 164.900000
0000003 102 164.900000
0000003 103 164.900000
0000003 106 161.800000
0000007 100 164.900000
0000007 102 164.900000
0000007 103 164.900000
0000007 106 161.800000
0000008 100 164.900000
0000008 102 164.900000
0000008 103 164.900000
0000008 106 161.800000
-------+-----+-------------+

Which I would like to Pivot like the following

Item Group100 Group102 Group103 Group106
-------+-------------+-------------+-----------------+--------------+
0000001 64.900000 164.900000 164.900000 161.800000
0000003 164.900000 164.900000 161.800000 161.800000
0000007 164.900000 161.800000 164.900000 164.900000
0000008 161.800000 161.800000 161.800000 161.800000
-------+-------------+-------------+-----------------+--------------+

Any help would be appreciated

thanks
Brendan

SwePeso
Patron Saint of Lost Yaks

Sweden
30217 Posts

Posted - 08/13/2013 :  03:21:00  Show Profile  Visit SwePeso's Homepage  Reply with Quote
DECLARE	@Sample TABLE
	(
		Item CHAR(7) NOT NULL,
		Grp TINYINT NOT NULL,
		Price MONEY NOT NULL
	);

INSERT	@Sample
	(
		Item,
		Grp,
		Price
	)
VALUES	('0000001', 102, 164.9000),
	('0000001', 103, 164.9000),
	('0000001', 106, 161.8000),
	('0000003', 100, 164.9000),
	('0000003', 102, 164.9000),
	('0000003', 103, 164.9000),
	('0000003', 106, 161.8000),
	('0000007', 100, 164.9000),
	('0000007', 102, 164.9000),
	('0000007', 103, 164.9000),
	('0000007', 106, 161.8000),
	('0000008', 100, 164.9000),
	('0000008', 102, 164.9000),
	('0000008', 103, 164.9000),
	('0000008', 106, 161.8000);

-- SwePeso
SELECT	p.Item,
	p.[100] AS Group100,
	p.[102] AS Group102,
	p.[103] AS Group103,
	p.[106] AS Group106
FROM	@Sample AS s
PIVOT	(
		MAX(s.Price)
		FOR s.Grp IN ([100], [102], [103], [106])
	) AS p;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000