code_junkie

Remapping/Concatenating in SQL

Nov 12th, 2011
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. id forename surname address
  2. 1 John These Address1
  3. 2 Lucy Values Address1
  4. 3 Jenny Are Address1
  5. 4 John All Address2
  6. 5 Lucy Totally Address2
  7. 6 Jenny Different Address2
  8. 7 Steve And Address2
  9. 8 Richard Blah Address2
  10.  
  11. address John Lucy Jenny Steve Richard
  12. Address1 These Values Are (null) (null)
  13. Address2 All Totally Different And Blah
  14.  
  15. select max(if(forename='john',surname,null)) as john,
  16. max(if(forename='lucy',surname,null)) as lucy,
  17. max(if(forename='jenny',surname,null)) as jenny,
  18. max(if(forename='steve',surname,null)) as steve,
  19. max(if(forename='richard',surname,null)) as richard,
  20. address
  21. from tablename
  22. group by address;
  23.  
  24. select address, group_concat( concat( forename, surname ) ) tenants
  25. from tablename
  26. group by address;
  27.  
  28. SELECT Address,Name FROM Table GROUP BY Name
  29.  
  30. SELECT concat(column1,column2,column3) as main_column, address from table;
Add Comment
Please, Sign In to add comment