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
 SQL Server Development (2000)
 Need help on Pivot

Author  Topic 

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2009-11-04 : 21:27:54
I've following table

t1
TID
----------
0001
0002
0003
0004
*TID is a unique

t2
TID | Cout | Posi
---------------------------
0001 | KT | 1
0001 | JB | 3
0002 | KT | 2
0002 | JB | 2
0003 | KT | 1
0003 | JB | 2
*Relationship between t1 and t2 is a 1 to many -- 1 record in t1, 2 record in t2

I want my resultset as follow,
TID | KT | JB
-------------------------------------------------
0001 | 1 | 3
0002 | 2 | 2
0003 | 1 | 2
0004 | null | null

How my SQL statement look's like? Me weak to play around with PIVOT

Need help

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-11-06 : 03:18:08
select
TID,
max(case when Cout='KJ' then Posi end) as KJ,
max(case when Cout='JB' then Posi end) as JB
from table2
group by TID


Madhivanan

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

- Advertisement -