Sql 当涉及日期列时,如何在Case语句中处理聚合函数
我正在计算invoiceDate vs currentDate,然后按客户分组求和值/金额列,但返回的“invoiceDate不包含在聚合函数或GROUP by子句中”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
选择客户
案例
当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,接受答案可能是感谢的好方法。