如何对SQL Server中的别名列执行GROUP BY?
我正在尝试对别名列(下例)执行GROUP BY操作,但无法确定正确的语法。
SELECT LastName + ', ' + FirstName AS 'FullName'
FROM customers
GROUP BY 'FullName'
正确的语法是什么?
进一步扩展问题(我没有预料到收到的答案)该解决方案是否仍适用于CASEed别名列?
SELECT
CASE
WHEN LastName IS NULL THEN FirstName
WHEN LastName IS NOT NULL THEN LastName + ', ' + FirstName
END AS 'FullName'
FROM customers
GROUP BY
LastName, FirstName
答案是肯定的,它仍然适用。
解决方案
您传递的是要分组的表达式,而不是别名
SELECT LastName + ', ' + FirstName AS 'FullName'
FROM customers
GROUP BY LastName + ', ' + FirstName
相关文章