可以使用条件聚合:
SELECT [Order],
MAX(CASE WHEN Area = 'Melt' THEN StartDate END) AS Melt_SDate,
MAX(CASE WHEN Area = 'Melt' THEN FinisthDate END) AS Melt_FDate,
MAX(CASE WHEN Area = 'Melt' THEN Weight END) AS Melt_Weight,
MAX(CASE WHEN Area = 'HSM' THEN StartDate END) AS HSM_SDate,
MAX(CASE WHEN Area = 'HSM' THEN FinisthDa END) AS HSM_FDate,
MAX(CASE WHEN Area = 'HSM' THEN Weight END) AS HSM_Weight,
MAX(CASE WHEN Area = 'LSM' THEN StartDate END) AS LSM_SDate,
MAX(CASE WHEN Area = 'LSM' THEN FinisthDate END) AS LSM_FDate,
MAX(CASE WHEN Area = 'LSM' THEN Weight END) AS LSM_Weight
FROM tab_name
GROUP BY [Order]; -- ORDER is reserved word, you should avoid such identifiers;
为了让它更简洁,你可以使用
IIF
:
MAX(CASE WHEN Area = 'Melt' THEN StartDate END) AS Melt_SDate,
<=>
MAX(IIF(Area='Melt',StartDate,NULL)) AS Melt_SDate,