sql >> Base de Datos >  >> RDS >> Sqlserver

valor de columna en una sola fila

Aquí hay dos métodos más, solo para completar:

  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'