1.12.08

Rows to columns in sql server

Its my third article on rows to columns coversion in sql server 2005. Now, its pretty different i have given a very different way to make this possible. Lot of our developers were finding to acheive this functionality in sql server 2000. Below is a simple approach to achieve it.

declare @Attendees table
( FunctionID integer,
DetailID integer,
ParticipantID integer,
StatusID integer
)
insert into @Attendees
select 1, 1, 123, 6 union all
select 1, 2, 124, 6 union all
select 1, 2, 125, 7
select * from @Attendees

declare @Status table
( StatusId integer,
Description varchar(10)
)
insert into @status
select 6, 'Invited' union all
select 7, 'Accepted'
select * from @status


select a.FunctionID,a.DetailID,--b.Description
case
when b.Description='Invited' then 1 else 0
end as invited,
case
when b.Description='Accepted' then 1 else 0
end as Accepted
from @Attendees a inner join
@status b on a.statusID=b.statusID


select a.FunctionID,a.DetailID,--b.Description
sum(case
when b.Description='Invited' then 1 else 0
end) as invited,
sum(case
when b.Description='Accepted' then 1 else 0
end) as Accepted
from @Attendees a inner join
@status b on a.statusID=b.statusID
group by a.FunctionID,a.detailid



Thanks and Regards,
Venkatesan Prabu .J

No comments:

Post a Comment