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 |
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2009-01-12 : 05:02:28
|
Table records : -------------------ID STATUS DATE 1 CL JAN11 OP FEB12 CL MAR12 OP APR1OUTPUT SHOULD BE ------------------ID CL_DATE OP_DATE1 JAN1 FEB12 MAR1 APR1 |
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-01-12 : 05:25:05
|
try thisselect id, case when status = 'cl' then date end as 'cl_status', case when status = 'op' then date end as 'op_status' from @tab-- it will not work in 2000 so works in 2005select id,[cl] ,[op]from @tab pivot(max(date)for status in ([op],[cl])) pvt |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-01-12 : 05:37:29
|
Hi try thisdeclare @table Table (id int,status varchar(88),date varchar(777)) insert into @tableselect 1, 'CL', 'JAN1' union allselect 1, 'OP', 'FEB1' union allselect 2, 'CL','MAR1' union allselect 2, 'OP', 'APR1'select id,max(cldate),max(opdate) from (select id ,case when status = 'cl' then date else null end as cldate,case when status = 'op' then date else null end as opdate from @table) t group by idJai Krishna |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-01-12 : 05:43:45
|
select id,max( case when status = 'cl' then date else null end) as cldate,max(case when status = 'op' then date else null end)as opdatefrom @table group by id |
|
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2009-01-12 : 05:45:36
|
HI JAY ,IT WORKS FINE ...but i could not understand how max(). group works in this scenario ??? |
|
|
niranjankumark
Posting Yak Master
164 Posts |
Posted - 2009-01-12 : 05:48:23
|
If the status is having empty date mean it is not displaying ...declare @table Table (id int,status varchar(88),date varchar(777)) insert into @tableselect 1, 'CL', 'JAN1' union allselect 1, 'OP', 'FEB1' union allselect 2, 'CL','MAR1' union allselect 2, 'CL','' union allselect 2, 'OP', 'APR1'SELECT * FROM @tableselect id ,case when status = 'cl' then date else null end as cldate,case when status = 'op' then date else null end as opdate from @tableselect id,max(cldate),max(opdate) from (select id ,case when status = 'cl' then date else null end as cldate,case when status = 'op' then date else null end as opdate from @table) t group by id |
|
|
|
|
|
|
|