Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LearnerID UnitID Completed
- 15 15 Yes
- 15 28 No
- 28 28 Yes
- 116 150 Yes
- 79 12 No
- 69 34 Yes
- 69 15 No
- LearnerID Unit 15 Unit 28 Unit 150 Unit 12 Unit 34
- 15 Yes No
- 28 Yes
- 116 Yes
- 79 Yes
- 69 No Yes
- select * into #tbl from (select 'cust1' as key1, 'Red'as Type
- union select 'cust1' as key1, 'Blue'as Type
- union select 'cust1' as key1, 'Yellow'as Type
- union select 'cust2' as key1, 'Red'as Type
- union select 'cust2' as key1, 'Blue'as Type
- union select 'cust2' as key1, 'Green'as Type) as dd
- declare @strsql as nvarchar(max)
- declare @columns as nvarchar(max)
- set @columns = (select stuff((select distinct ',['+Type+']' from #tbl FOR XML PATH('')),1,1,''))
- set @strsql = 'select key1,'+@columns + 'from (select * from #tbl) as p pivot (count(p.Type) For p.Type in ('+@columns+'))as pivottable'
- --set @strsql = 'select
- EXECUTE sp_executesql @strsql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement