我创建了下表(通过 UNION ALL):
ID Date Hour Weight Fiscal
AAA 1/27/2009 0 10 0
AAA 1/30/2009 0 20 0
AAA 2/14/2009 0 10 0
AAA 2/18/2009 0 20 0
AAA 2/27/2009 0 20 0
AAA 2/28/2009 0 20 0
AAA 1/14/2009 10 0 0
AAA 2/14/2009 20 0 0
AAA 2/16/2009 10 0 0
AAA 2/25/2009 10 0 0
AAA 2/26/2009 10 0 0
AAA 3/3/2009 20 0 0
NULL 0 0 0 1/28/2009
NULL 0 0 0 2/27/2009
NULL 0 0 0 3/29/2009
我想检索:
ID Date Hour Weight
AAA 1/28/2009 10 10
AAA 2/27/2009 50 50
AAA 3/29/2009 20 20
它应该首先将 DATE 列转换为下一个会计月末 FISCAL(例如 1/27/2007 -> 1/28/2009、1/30/2009 -> 2/27/2009 等),然后按那个值。
我能够使用:
SELECT ID
, left(CONVERT(VARCHAR(10)
, date, 111),7) as Date
, round(sum(Hours),0) as Hours
, round(sum(Weight),0) as Weight
FROM ...
GROUP BY ID
, left(CONVERT(VARCHAR(10), date, 111),7)
ORDER by ID ASC
, left(CONVERT(VARCHAR(10), date, 111),7) ASC
但这只是按日历日期分组,而不是根据我在FISCAL列中的财政月份结束。