Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Righe concatenate da una colonna calcolata alias

Questo funziona per me (sarebbe più facile se fornissi alcuni dati con cui testare):

SELECT
  C.CarName,
  SUBSTRING((
             SELECT
              ', ' + CAST(CarMileage / TotalGasTrips AS VARCHAR(MAX))
             FROM
              Mileage
             WHERE
              Mileage.CarName = C.CarName
             GROUP BY
              dbo.Mileage.CarMileage,
              dbo.Mileage.TotalGasTrips
            FOR
             XML PATH('')
            ), 2, 1000) AS MPGString
FROM
  Car C
  JOIN dbo.Mileage M ON C.CarName = M.CarName
GROUP BY
  C.CarName