简体   繁体   English

SQL查询聚合出错

[英]Getting an error with SQL Query Aggregation

I am getting this error: 我收到此错误:

Cannot perform an aggregate function on an expression containing an aggregate or a sub-query 无法对包含聚集或子查询的表达式执行聚集功能

Below is part of my SQL query_ 以下是我的SQL query_的一部分

SUM((CASE 
        WHEN (SELECT [Amount] FROM [Transaction_table] 
              WHERE [Receipt No_] = se.[Receipt No_]) IS NULL
           THEN 1 
           ELSE [Amount] 
     END)) 'Total Amount'

I think ANSI sql standard coalesce() will do that you want 我认为ANSI sql标准coalesce()您的要求

select sum(coalesce([Amount],1)) [Total Amount] from [Transaction_table]
where [Receipt No_]=se.[Receipt No_]

other way by case expression will do the same thing 通过case表达的其他方式将做同样的事情

select sum(case when [Amount] is null then 1 else [Amount] end) [Total Amount]
from [Transaction_table]
where [Receipt  No_] = se.[Receipt No_];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM