Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 当涉及日期列时,如何在Case语句中处理聚合函数_Sql_Sql Server - Fatal编程技术网

Sql 当涉及日期列时,如何在Case语句中处理聚合函数

Sql 当涉及日期列时,如何在Case语句中处理聚合函数,sql,sql-server,Sql,Sql Server,我正在计算invoiceDate vs currentDate,然后按客户分组求和值/金额列,但返回的“invoiceDate不包含在聚合函数或GROUP by子句中” 选择客户 案例 当datediff(dd、InvoiceDate、getdate())需要条件聚合时: select Customer sum(case when datediff(day, InvoiceDate, getdate()) <= 30 then Inv

我正在计算invoiceDate vs currentDate,然后按客户分组求和值/金额列,但返回的“invoiceDate不包含在聚合函数或GROUP by子句中”

选择客户
案例

当datediff(dd、InvoiceDate、getdate())需要条件聚合时:

select Customer     
       sum(case when datediff(day, InvoiceDate, getdate()) <= 30
                then InvoiceBal1 else 0
           end) as balance_current
       sum(case when datediff(day, InvoiceDate, getdate()) between 31 and 60
                then InvoiceBal1 else 0
           end) as balance_30days
from CusInvoice
group by Customer
选择客户

求和(datediff(day、InvoiceDate、getdate())需要条件聚合时的大小写)

select Customer     
       sum(case when datediff(day, InvoiceDate, getdate()) <= 30
                then InvoiceBal1 else 0
           end) as balance_current
       sum(case when datediff(day, InvoiceDate, getdate()) between 31 and 60
                then InvoiceBal1 else 0
           end) as balance_30days
from CusInvoice
group by Customer
选择客户

总和(datediff(day、InvoiceDate、getdate()时的大小写)你的问题被标记为MySQL,但你的代码看起来更像SQL Server。我更改了标记。你的问题被标记为MySQL,但你的代码看起来更像SQL Server。我更改了标记。嗨@Ronald,接受答案可能是感谢的好方法。嗨@Ronald,接受答案可能是感谢的好方法。