Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. Result Type Type Order Type Value
  2. --------------------------------------
  3. KM1 Color 1 Blue
  4. KM1 Shape 2 Square
  5. KM1 Size 3 10
  6. KM3 Color 1 Blue
  7. KM3 Shape 2 Square
  8. KM3 Size 3 10
  9. KM2 Color 1 Red
  10. KM2 Shape 2 Round
  11. KM2 Size 3 20
  12. KM2 Color 1 Blue
  13. KM2 Shape 2 Square
  14. KM2 Size 3 10
  15. KM2 Color 1 Blue
  16. KM2 Shape 2 Round
  17. KM2 Size 3 10
  18.  
  19. Color Shape Size Result
  20. -------------------------------------
  21. Blue Square 10 KM1, KM3, KM2
  22. Red Round 20 KM2
  23. Blue Round 10 KM2
  24.  
  25. with t as (
  26. select result,
  27. max(case when type = 'Color' then value end) as color,
  28. max(case when type = 'Size' then value end) as size,
  29. max(case when type = 'Shape' then value end) as shape
  30. from t
  31. group by result
  32. )
  33. select color, size, shape,
  34. stuff( (select ',' + t2.result
  35. from t t2
  36. where t2.color = t.color and t2.size = t.size and t2.shape = t.shape
  37. for xml path ('')
  38. ), 1, 1, '') as keys
  39. from (select distinct color, size, shape
  40. from t
  41. ) t;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement