Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. LearnerID UnitID Completed
  2. 15 15 Yes
  3. 15 28 No
  4. 28 28 Yes
  5. 116 150 Yes
  6. 79 12 No
  7. 69 34 Yes
  8. 69 15 No
  9.  
  10. LearnerID Unit 15 Unit 28 Unit 150 Unit 12 Unit 34
  11. 15 Yes No
  12. 28 Yes
  13. 116 Yes
  14. 79 Yes
  15. 69 No Yes
  16.  
  17. select * into #tbl from (select 'cust1' as key1, 'Red'as Type
  18. union select 'cust1' as key1, 'Blue'as Type
  19. union select 'cust1' as key1, 'Yellow'as Type
  20. union select 'cust2' as key1, 'Red'as Type
  21. union select 'cust2' as key1, 'Blue'as Type
  22. union select 'cust2' as key1, 'Green'as Type) as dd
  23.  
  24. declare @strsql as nvarchar(max)
  25. declare @columns as nvarchar(max)
  26.  
  27. set @columns = (select stuff((select distinct ',['+Type+']' from #tbl FOR XML PATH('')),1,1,''))
  28. set @strsql = 'select key1,'+@columns + 'from (select * from #tbl) as p pivot (count(p.Type) For p.Type in ('+@columns+'))as pivottable'
  29. --set @strsql = 'select
  30. EXECUTE sp_executesql @strsql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement