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 2000 Forums
 Transact-SQL (2000)
 Grouping Data

Author  Topic 

ladiaocb
Yak Posting Veteran

57 Posts

Posted - 2007-01-29 : 14:50:35
I have the following table:

t1=

id--------dataid-------datavalue
1000------1------------supervisor1
1000------2------------203
1000------3------------20060101

The end result I am trying to come up with is:

result =

id------supervisor-------roomid-------hiredate
1000----supervisor1------203----------20060101

The id column represents a userid. Dataid 1 represents a supervisorname. Dataid 2 represents a room number. Dataid 3 represents a hiredate.
I tried grouping the data but its not working the way I'd like. Any help is always appreciated!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-29 : 14:57:41
select id, max(case when dataid = 1 then datavalue end) as supervisor,
max(case when dataid = 2 then datavalue end) as roomid,
max(case when dataid = 3 then datavalue end) as hiredate
from t1
group by id


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2007-01-29 : 15:04:10
Please provide with another data set or 2 and the expected results as well.
Its not clear whether the dataID is 1 for all supervisor names etc ...

Also, ur database is not normalized.



Srinika
Go to Top of Page

ladiaocb
Yak Posting Veteran

57 Posts

Posted - 2007-01-29 : 15:06:41
This works perfect! Thanks much!
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-01-29 : 15:24:29
Another EAV design.

The horror! The horror!




CODO ERGO SUM
Go to Top of Page
   

- Advertisement -