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 |
skwash
Starting Member
2 Posts |
Posted - 2009-07-17 : 17:17:44
|
Would it be possible to create a pivot table using non-aggregate data?Source table would look like:ID Type Value---------------------1 Name Sam1 Sex M1 Phone 1232 Name Sally2 Sex F3 Name Pat4 Name Tom4 Phone 456Result would look like this:ID Name Sex Phone---------------------------1 Sam M 1232 Sally F Null3 Pat Null Null4 Tom Null 456Does that make sense? I am open to doing the "pivot table" sort of action in Excel or even installing reporting services if I can get it at all.Thanks,Josh |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-07-17 : 17:30:19
|
Yes, you can, but it will be awful slow compared to the PIVOT function. N 56°04'39.26"E 12°55'05.63" |
|
|
skwash
Starting Member
2 Posts |
Posted - 2009-07-17 : 17:55:46
|
What would you recommend? PIVOT requires aggregate functions correct?The only way can think of doing it is through a big nasty dynamic sql statement. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-18 : 00:30:02
|
[code]declare @sample table( ID int, Type varchar(10), Value varchar(10))insert into @sampleselect 1, 'Name', 'Sam' union allselect 1, 'Sex', 'M' union allselect 1, 'Phone', '123' union allselect 2, 'Name', 'Sally' union allselect 2, 'Sex', 'F' union allselect 3, 'Name', 'Pat' union allselect 4, 'Name', 'Tom' union allselect 4, 'Phone', '456'select *from @sample s pivot ( max(Value) for Type in ([Name], [Sex], [Phone]) ) p[/code] KH[spoiler]Time is always against us[/spoiler] |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-18 : 02:10:45
|
or simply doSELECT ID,MAX(CASE WHEN Type='Name' THEN Value ELSE NULL END) AS [Name],MAX(CASE WHEN Type='Sex' THEN Value ELSE NULL END) AS [Sex],MAX(CASE WHEN Type='Phone' THEN Value ELSE NULL END) AS [Phone]FROM TableGROUP BY ID |
|
|
|
|
|
|
|