Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

valeur de colonne dans une seule ligne

Voici deux autres méthodes, juste pour être complet :

  1. SELECT
      p.name,
      MAX(CASE t.phonetype WHEN 'work' THEN t.phoneno END) AS workphone,
      MAX(CASE t.phonetype WHEN 'home' THEN t.phoneno END) AS homephone,
      MAX(CASE t.phonetype WHEN 'mob' THEN t.phoneno END) AS mobilephone
    FROM people p
      INNER JOIN phones t ON p.Id = t.Id
    GROUP BY p.Id, p.Name
    
  2. SELECT
      p.name,
      w.phoneno AS workphone,
      h.phoneno AS homephone,
      m.phoneno AS mobilephone
    FROM people p
      LEFT JOIN phones w ON p.Id = w.Id AND w.phonetype = 'work'
      LEFT JOIN phones h ON p.Id = h.Id AND h.phonetype = 'home'
      LEFT JOIN phones m ON p.Id = m.Id AND m.phonetype = 'mob'