我在下面的查询中遇到了一些问题。我正在尝试确定“类别”字段是 A、B 还是 C,然后根据类别创建一个字段。该字段将总结支付字段。但是我遇到了错误,说“关键字 As 附近的语法不正确”。我在 SQL 视图中创建它。使用 SQL Server 2008
SELECT r.id, r.category
CASE
WHEN r.category = 'A' then SUM(r.payment) As A_payments
WHEN r.category = 'B' then SUM(r.payment) As B_payments
WHEN r.category = 'C' then SUM(r.payment) As C_payments
END
FROM r_invoiceTable As r
GROUP BY r.id, r.category
我有数据应该执行上述所有情况,因为我拥有的数据有 A、B 和 C
样本数据 - r_invoiceTable
Id --- Category ---- Payment
222 A ---- 50
444 A ---- 30
111 B ---- 90
777 C ---- 20
555 C ---- 40
期望的输出 A_payments = 80, B_payments = 90, C_payments = 60