我正在尝试对某些数据进行年龄分析,并且需要进行条件求和,即我的表是:
ID | Date | Amount |
===+=========+========+
1 | 1/1/10 | 100 |
2 | 1/2/10 | 100 |
3 | 1/5/10 | 100 |
4 | 15/5/10 | 100 |
5 | 20/5/10 | 100 |
假设今天的日期是1/6/10,我想根据年龄分析中所使用的年龄来求和。即我想要这个
Age | Total
===========+======
<30 days | 300
30-60 days | 0
60-90 days | 0
90 days+ | 200
本质上是有条件的求和,所以我想对所有值求和(
最佳答案
你可以做:
select case
when datediff(now(), date) >= 90 then '90 days+'
when datediff(now(), date) >= 60 and datediff(now(), date) < 90 then '60-90 days'
when datediff(now(), date) >= 30 and datediff(now(), date) < 60 then '30-60 days'
else '< 30 days'
end case f,
sum(amount)
from your_table
group by f;
关于mysql - mysql条件求和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8021192/