I’m calculating invoiceDate vs currentDate then sum Value/Amount column grouping by Customer but its returning “invoiceDate is not contained in either an aggregate function or the GROUP BY clause”
select Customer ,case when datediff(dd,InvoiceDate,getdate()) <=30 then sum(InvoiceBal1) else 0 end as [Current] ,case when datediff(dd,InvoiceDate,getdate()) between 31 and 60 then sum(InvoiceBal1) else 0 end as [30 Days] from CusInvoice group by Customer
Advertisement
Answer
You want conditional aggregation:
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