How do I perform a GROUP BY on an aliased column in MS-SQL Server?

后端 未结 12 1841
慢半拍i
慢半拍i 2020-11-29 00:20

I\'m trying to perform a group by action on an aliased column (example below) but can\'t determine the proper syntax.

SELECT       LastName + \', \'         


        
12条回答
  •  孤独总比滥情好
    2020-11-29 01:00

    For anyone who finds themselves with the following problem (grouping by ensuring zero and null values are treated as equals)...

    SELECT AccountNumber, Amount AS MyAlias
    FROM Transactions
    GROUP BY AccountNumber, ISNULL(Amount, 0)
    

    (I.e. SQL Server complains that you haven't included the field Amount in your Group By or aggregate function)

    ...remember to place the exact same function in your SELECT...

    SELECT AccountNumber, ISNULL(Amount, 0) AS MyAlias
    FROM Transactions
    GROUP BY AccountNumber, ISNULL(Amount, 0)
    

提交回复
热议问题