我有一个表partywise_account_ledger
,其中包含name_of_the_customer
,invoice_date
和invoice_value
列。我想根据天数检索invoice_value
。要获取< 5 days
的值,我使用查询:
select name_of_the_customer, sum(invoice_value)
from partywise_accounts_ledger
where datediff(d, invoice_date, '2017-04-19') < 5
group by name_of_the_customer
This is the Result of the Query
如何将< 10 days
列附加到其中并在单个语句中显示?
谢谢!
答案 0 :(得分:1)
将case
表达式用于条件聚合:
select name_of_the_customer,
sum(case when datediff(d, invoice_date, '2017-04-19') < 5 then invoice_value end),
sum(case when datediff(d, invoice_date, '2017-04-19') < 10 then invoice_value end)
from partywise_accounts_ledger
where datediff(d, invoice_date, '2017-04-19') < 10
group by name_of_the_customer
第二个SUM()
(适用于<10天),可以简化为sum(invoice_value)
。