Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Buzzard724
Yak Posting Veteran
66 Posts |
Posted - 2010-02-18 : 05:37:59
|
| I want to create a pivot table with if possible two rows per person - one for the oldvalue and one for the newvalue. Script to create an example is below - including a Select/Pivot statement which produces a row per record (help!).I want the result set to look as follows (oldvalue in the first row for each person and newvalue in the second row for each person)FullName Address1 Phone Period JobTitleAndrea Summer NULL 5673 NULL TesterAndrea Summer 22 Road 234 NULL ProverJohn Edge NULL 234563456 NULL GrowerJohn Edge 47 Crescent 875876456 NULL FarmerPeter James 33 Street 1234 NULL AssistantPeter James 34 Street 2345 NULL Boss/*****************Create a Pivot table*****************/CREATE TABLE [dbo].[PeopleValues]([PeopleValues_ID] [uniqueidentifier] NOT NULL,[FieldName] [nvarchar](30) NULL,[People_ID] [uniqueidentifier] NOT NULL,[FullName] [nvarchar](30) NULL,[OldValue] [nvarchar](30) NULL,[NewValue] [nvarchar](30) NULL)INSERT INTO [dbo].[PeopleValues]([PeopleValues_ID], [FieldName], [People_ID],[FullName],[OldValue],[NewValue])SELECT NewID(), 'Address1', '311ADD0C-CC8A-45E3-8AEF-0A77957F9B56', 'Peter James', '33 Street', '34 Street' UNION ALLSELECT NewID(), 'Address1', '4E5EDE5C-3A22-4E73-8C0D-0AD8F9F00402', 'Andrea Summer', Null, '22 Road' UNION ALLSELECT NewID(), 'Address1', 'C85BA2EE-CC95-4C61-AF83-108244555EDB', 'John Edge', Null, '47 Crescent' UNION ALLSELECT NewID(), 'Phone', '311ADD0C-CC8A-45E3-8AEF-0A77957F9B56', 'Peter James', '1234', '2345' UNION ALLSELECT NewID(), 'Phone', '4E5EDE5C-3A22-4E73-8C0D-0AD8F9F00402', 'Andrea Summer', '5673', '234' UNION ALLSELECT NewID(), 'Phone', 'C85BA2EE-CC95-4C61-AF83-108244555EDB', 'John Edge', '234563456', '875876456' UNION ALLSELECT NewID(), 'JobTitle', '311ADD0C-CC8A-45E3-8AEF-0A77957F9B56', 'Peter James', 'Assistant', 'Boss' UNION ALLSELECT NewID(), 'JobTitle', '4E5EDE5C-3A22-4E73-8C0D-0AD8F9F00402', 'Andrea Summer', 'Tester', 'Prover' UNION ALLSELECT NewID(), 'JobTitle', 'C85BA2EE-CC95-4C61-AF83-108244555EDB', 'John Edge', 'Grower', 'Farmer'--And the following which does a bad partial job !!SELECT * FROM [PeopleValues]PIVOT( Max(OldValue) FOR [FieldName] IN ([Address1],[Phone], [Period], [JobTitle]))AS pUNION ALLSELECT * FROM [PeopleValues]PIVOT( Max(NewValue) FOR [FieldName] IN ([Address1],[Phone], [Period], [JobTitle]))AS pOrder by fullname--thank you |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-18 : 05:48:22
|
| [code]SELECT * FROM (SELECT [FieldName],[FullName],[OldValue] FROM [PeopleValues])tPIVOT(Max(OldValue)FOR [FieldName] IN ([Address1],[Phone], [Period], [JobTitle]))AS pUNION ALLSELECT * FROM (SELECT [FieldName],[FullName],[NewValue] FROM [PeopleValues])tPIVOT(Max(NewValue)FOR [FieldName] IN ([Address1],[Phone], [Period], [JobTitle]))AS pOrder by fullnameoutput------------------------------FullName Address1 Phone Period JobTitleAndrea Summer NULL 5673 NULL TesterAndrea Summer 22 Road 234 NULL ProverJohn Edge 47 Crescent 875876456 NULL FarmerJohn Edge NULL 234563456 NULL GrowerPeter James 33 Street 1234 NULL AssistantPeter James 34 Street 2345 NULL Boss[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Buzzard724
Yak Posting Veteran
66 Posts |
Posted - 2010-02-18 : 06:01:49
|
| Thank you Visakh - exactly what I wanted. Is it possible to do this without hand coding the values from FieldName in the For clause? again thank you |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
Buzzard724
Yak Posting Veteran
66 Posts |
Posted - 2010-02-18 : 06:13:07
|
| OK - so now my ignorance about stored procedures - is it possible to use code like the example in your blog - to construct a View - in other words could this be a View Script?CREATE VIEW DPRecords ASEXEC dynamic_pivotSELECT e.lastname, o.OrderDate FROM northwind..Employees as eINNER JOIN northwind..Orders as o ON (e.EmployeeID=o.EmployeeID) ',Year(OrderDate)',Count(OrderDate)' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-18 : 06:46:02
|
quote: Originally posted by Buzzard724 OK - so now my ignorance about stored procedures - is it possible to use code like the example in your blog - to construct a View - in other words could this be a View Script?CREATE VIEW DPRecords ASEXEC dynamic_pivotSELECT e.lastname, o.OrderDate FROM northwind..Employees as eINNER JOIN northwind..Orders as o ON (e.EmployeeID=o.EmployeeID) ',Year(OrderDate)',Count(OrderDate)'
Nope. you need to return it as a stored procedure resultset------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|