Я думаю, вам не нужно использовать ROW_NUMBER()
, используйте AdminFileId
только в предложении GROUP BY
будет иметь только одну запись:
SELECT al.AdminFileId,
MAX(CASE WHEN al.LanguageLevelId = 4 THEN l.Label END) AS MotherTongue,
MAX(CASE WHEN al.LanguageLevelId = 2 THEN l.Label END) AS Fluent
FROM AF_Language al LEFT JOIN
AF_AdminFile aaf
ON aaf.AdminFileId = al.AdminFileId LEFT JOIN -- Used LEFT JOIN INSTEAD OF INNER
Employee e
ON e.AdminFileId = aaf.AdminFileId LEFT JOIN
Language l
ON al.LanguageId = l.ID
GROUP BY al.AdminFileId;
РЕДАКТИРОВАТЬ: Использование row_number
:
SELECT al.AdminFileId, l.Label,
ROW_NUMBER() OVER (PARTITION BY al.AdminFileId
ORDER BY (CASE WHEN al.LanguageLevelId = 4
THEN 1 ELSE 2
END)
) AS Seq
FROM AF_Language al LEFT JOIN
AF_AdminFile aaf
ON aaf.AdminFileId = al.AdminFileId LEFT JOIN -- Used LEFT JOIN INSTEAD OF INNER
Employee e
ON e.AdminFileId = aaf.AdminFileId LEFT JOIN
Language l
ON al.LanguageId = l.ID
WHERE al.LanguageLevelId IN (4, 2);
Тогда вы можете использовать подзапрос:
SELECT AdminFileId,
MAX(CASE WHEN Seq = 1 THEN Label END) AS MotherTongue,
MAX(CASE WHEN Seq = 2 THEN Label END) AS Fluent
FROM ( <Query>
) t
GROUP BY AdminFileId;