Saturday, June 29, 2013

rows data in column

Hi,


Try this query:



IF OBJECT_ID('tempdb..#Test') is not null
drop table #test

create table #Test
( Code varchar(10),
CodeProperty varchar(100)
)

insert into #Test(Code,CodeProperty) Values
('GP1','Virus Protection'),
('GP1','Annual Desktop Support 1yr'),
('GP1','Online Data Backup'),
('GP1','TuneUp Utilities 2013'),
('PP3','Virus Protection'),
('PP3','Online Data Backup'),
('PP3','TuneUp Utilities 2013'),
('PP3','Annual Desktop Support 3yr'),
('AP1','Virus Protection'),
('AP1','Annual Desktop Support 1yr'),
('AP1','Online Data Backup'),
('A014','Annual Desktop Support 3yr'),
('PP1','Single incident'),
('GP3.1','Annual Desktop Support 3yr'),
('A027','Annual Desktop Support 3yr'),
('GP3','Annual Desktop Support 3yr'),
('A024','Online Data Backup'),
('A025','Annual Desktop Support 1yr'),
('A002','Annual Desktop Support 1yr'),
('A018','Virus Protection'),
('A011','Annual Desktop Support 2yr'),
('GP2','Virus Protection'),
('GP2','Online Data Backup'),
('GP2','TuneUp Utilities 2013'),
('GP2','Annual Desktop Support 2yr'),
('A010','Annual Desktop Support 1yr'),
('A016','Annual Desktop Support 2yr'),
('A013','Annual Desktop Support 3yr'),
('GP2.1','Annual Desktop Support 2yr'),
('A012','Annual Desktop Support 2yr'),
('A021','TuneUp Utilities 2013'),
('A026','Annual Desktop Support 2yr'),
('A017','Annual Desktop Support 3yr'),
('A015','Annual Desktop Support 1yr'),
('A1Y','Virus Protection'),
('A1Y','Annual Desktop Support 1yr'),
('A1Y','Online Data Backup'),
('A1Y','TuneUp Utilities 2013')

SELECT code ,
CASE WHEN [Annual Desktop Support 1yr] > 0 THEN 'Y' ELSE 'N' END AS [Annual Desktop Support 1yr]
,CASE WHEN [Annual Desktop Support 2yr] > 0 THEN 'Y' ELSE 'N' END AS [Annual Desktop Support 2yr]
,CASE WHEN [Annual Desktop Support 3yr] > 0 THEN 'Y' ELSE 'N' END AS [Annual Desktop Support 3yr]
,CASE WHEN [Online Data Backup] > 0 THEN 'Y' ELSE 'N' END AS [Online Data Backup]
,CASE WHEN [Single incident] > 0 THEN 'Y' ELSE 'N' END AS [Single incident]
,CASE WHEN [TuneUp Utilities 2013] > 0 THEN 'Y' ELSE 'N' END AS [TuneUp Utilities 2013]
,CASE WHEN [Virus Protection] > 0 THEN 'Y' ELSE 'N' END AS [Virus Protection]
from
(SELECT Code, CodeProperty FROM #test) AS SRC
PIVOT(
COUNT(CodeProperty)
FOR CodeProperty IN ([Annual Desktop Support 1yr]
,[Annual Desktop Support 2yr]
,[Annual Desktop Support 3yr]
,[Online Data Backup]
,[Single incident]
,[TuneUp Utilities 2013]
,[Virus Protection])
) AS pvt





Regards Harsh


No comments:

Post a Comment