我有一个问题,我想通过日期合并我的表格数据,所有信息都在给定的表格中提供
如果Aggregation = Semiannual
我的表
表1
Amount | TheDate | Aggregation
-------+----------------+-------------
100 | 2013-01-01 | Quaterly
100 | 2013-02-01 | Quaterly
100 | 2013-03-01 | Quaterly
100 | 2013-04-01 | Quaterly
100 | 2013-05-01 | Quaterly
100 | 2013-06-01 | Quaterly
200 | 2013-07-01 | Quaterly
200 | 2013-07-01 | Quaterly
200 | 2013-09-01 | Quaterly
200 | 2013-10-01 | Quaterly
200 | 2013-11-01 | Quaterly
200 | 2013-12-01 | Quaterly
我想要这样的结果
Amount | Date | Aggregation
-------+-------------+-------------
600 | 2013-06-01 | Quaterly
1200 | 2013-12-01 | Quaterly
查询:
SELECT
DATEADD(MONTH, 2, DATEADD(quarter, DATEDIFF(quarter, 0, TheDate), 0)) AS Amount,
ROUND(CONVERT(FLOAT, SUM(Amount)) / 1000, 0) AS Total
FROM
myTble
GROUP BY
DATEADD(quarter, DATEDIFF(QQ, 0, TheDate), 0)
我正在尝试上面的查询,但它只适用于季度
答案 0 :(得分:4)
您应该按年份和(MONTH-1)/6 = 0 for [1-6] or 1 for [7-12]
SELECT SUM(Amount),
MAX(TheDate)
FROM T
GROUP BY YEAR(TheDate),(MONTH(TheDate)-1)/6