Posted By

built2fall on 10/10/10


Tagged

sql rows combine pivot groupconcat


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Tyster


Combine Multiple Rows into a Single Row


 / Published in: SQL
 

  1. /*
  2. name | stat | value
  3. ------------------------
  4. object1 | stat1 | val1
  5. object1 | stat2 | val2
  6. object1 | stat3 | val3
  7.  
  8. To
  9.  
  10. name | stat1 | stat2 | stat3
  11. -------------------------------
  12. object1 | val1 | val2 | val3
  13.  
  14. */
  15.  
  16. SELECT name
  17. , MAX(CASE WHEN stat = 'stat1' THEN value ELSE NULL END) AS stat1
  18. , MAX(CASE WHEN stat = 'stat2' THEN value ELSE NULL END) AS stat2
  19. , MAX(CASE WHEN stat = 'stat3' THEN value ELSE NULL END) AS stat3
  20. FROM TABLE
  21. GROUP BY name

Report this snippet  

You need to login to post a comment.