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 2005 Forums
 Transact-SQL (2005)
 Help to write the following query

Author  Topic 

Sarakumar
Posting Yak Master

108 Posts

Posted - 2009-05-10 : 22:08:22
declare @Customer table
(cid int,
item varchar(50),
in_use bit)
insert into @Customer values (1,'mobile',0)
insert into @Customer values (1,'TV',1)
insert into @Customer values(1,'Comp',1)
insert into @Customer values(1,'Laptop',0)
insert into @Customer values(2,'mobile',0)
insert into @Customer values(2,'TV',1)
insert into @Customer values(2,'Comp',0)
insert into @Customer values(2,'Laptop',1)

SELECT cid, [mobile],[TV], [Comp],[Laptop]from
(select cid, item ,in_use from @Customer ) st
pivot
(count(item) for item in([mobile],[TV], [Comp],[Laptop]))PT
order by cid

Hai, With the help of our people in the forum, i could write the above pivot query..But now, i have added new column called in_use in the table,
an i want to result to be like the following..

cid mobile tv comp laptop
1 no yes yes NO
2 No Yes No Yes

i dont know how to write the query.Help pls

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-10 : 22:13:15
change to

(
select cid, item
from @Customer
where in_use = 1
) st



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Sarakumar
Posting Yak Master

108 Posts

Posted - 2009-05-10 : 22:31:02
Thanks...it works...
Go to Top of Page
   

- Advertisement -