日期范围的SQL查询为

时间:2016-12-15 22:01:32

标签: sql date ms-access

我有以这种格式存储的数据:

Name | Order Date | Order Amount
---  

我需要它显示如下:

Name | Jun 16-30 | Jul | Aug | Sep | Oct | ... | May | Jun 1 - 15
---

订单金额在每个日期范围/月下汇总。

有没有办法通过枢轴来实现这一点,或者与案例相加?我需要总结日期之间的日/月,但年份并不重要。

1 个答案:

答案 0 :(得分:2)

在MS Access中使用条件聚合的一般格式是:

select name,
       sum(iif(datepart("month", OrderDate) 6 and datepart("day", OrderDate) between 16 and 30, OrderAmount, 0)) as [Jun 16-30],
       sum(iif(datepart("month", OrderDate) = 7, OrderAmount, 0) as [Jul],
       . . .
from t
group by name;
相关问题