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 2008 Forums
 Transact-SQL (2008)
 Help on a pivot-like table

Author  Topic 

vgarzon
Starting Member

11 Posts

Posted - 2009-10-29 : 11:25:58
Hi all,

I'm having issues trng to pivot a table that has the following structure:
Employee ID, Date, Field, New Value, Old Value

Supposing that in the "Field" field can have 20 different values, and that every time a field is updated, we record only the updated field in the table.

I have to show the data with the following structure:
Employee ID, Field 1, Field 2, ...., Field 20
Showing only the latest version of the Employee's information.

Was I clear enough? jeje.

Simplifying things, I have:

ID DATE FIELD OLD NEW
1 2009-01-01 00:00:00.000 A NULL new1
1 2009-01-01 00:00:00.000 B NULL new2
1 2009-01-01 00:00:00.000 C NULL new3
1 2009-01-01 00:00:00.000 D NULL new4
2 2009-01-01 00:00:00.000 A NULL bla1
2 2009-01-01 00:00:00.000 B NULL bla2
2 2009-01-01 00:00:00.000 C NULL bla3
2 2009-01-01 00:00:00.000 D NULL bla4
3 2009-03-01 00:00:00.000 A NULL asd1
3 2009-03-01 00:00:00.000 B NULL asd2
3 2009-03-01 00:00:00.000 C NULL asd3
3 2009-03-01 00:00:00.000 D NULL asd4
1 2009-05-05 00:20:00.000 C new3 OPA

And I have to show:

A B C D
1 new1 new2 OPA new4
2 bla1 bla2 bla3 bla4
3 asd1 asd2 asd3 asd4

Any ideas of how to do this?

Thanks in advance,
Victor

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-23 : 09:30:12
Refer
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -