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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Can Pivot solve this?

Author  Topic 

tmarko
Starting Member

15 Posts

Posted - 2008-07-23 : 05:23:55
I'm new to SQL2005, but looked into the pivot function a bit, but cannot solve this anyway. Do not know if it's possible?

My table lokks like this (Columns)

CompanyId, TimeId, KeyValue1, KeyValue2, KeyValue3 ....

Now I would like the values to show with these columns

CompanyId, KeyValue1(TimeId1),KeyValue(TimeId2), KeyValue2(TimeId1),Keyvalue2(TimeId2) ...

Eliminating the TimeId column and
choose specific TimeId values for specific Keyvalues and make it into one column

Hope my question is quite clear

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-23 : 05:32:11
you can either use pivot or this:-

SELECT CompanyId,
MAX(CASE WHEN TimeId='TimeId1' THEN KeyValue1 ELSE NULL END) AS [KeyValue1(TimeId1)],
MAX(CASE WHEN TimeId='TimeId2' THEN KeyValue1 ELSE NULL END) AS [KeyValue1(TimeId2)],
MAX(CASE WHEN TimeId='TimeId1' THEN KeyValue2 ELSE NULL END) AS [KeyValue2(TimeId1)],
MAX(CASE WHEN TimeId='TimeId2' THEN KeyValue2 ELSE NULL END) AS [KeyValue2(TimeId2)],...
FROM YourTable
GROUP BY CompanyId
Go to Top of Page
   

- Advertisement -