我想知道从我最初的想法到哪里去。我使用下面的查询来获取三年中每一年的月份开始日期:
DECLARE @STARTDATE DATETIME,
@ENDDATE DATETIME;
SELECT @STARTDATE='2013-01-01 00:00:00.000',
@ENDDATE='2015-12-31 00:00:00.000';
WITH [3YearDateMonth]
AS
(
SELECT TOP (DATEDIFF(mm,@STARTDATE,@ENDDATE) + 1)
MonthDate = (DATEADD(mm,DATEDIFF(mm,0,@STARTDATE) + (ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1),0))
FROM sys.all_columns ac1
)
SELECT MonthDate
FROM [3YearDateMonth]
我不确定我是否应该稍后将 DATENAME(Month, Monthdate) 用于月份名称,或者只是在 cte 中进行;任何建议都会很棒。
我的数据如下所示:
BeginDate EndDate Payment
2013-01-01 00:00:00.000 2013-12-31 00:00:00.000 3207.70
2014-01-01 00:00:00.000 2014-12-31 00:00:00.000 3303.93
2015-01-01 00:00:00.000 2015-12-31 00:00:00.000 3403.05
由于付款是每年一次,我可以使用 payment/12 来获得平均每月金额。我希望我的数据看起来像这样:
BeginDate EndDate Month MonthlyAmount
2013-01-01 00:00:00.000 2013-01-31 00:00:00.000 January 267.3083
2013-02-01 00:00:00.000 2013-02-31 00:00:00.000 February 267.3083
...
2014-01-01 00:00:00.000 2014-01-31 00:00:00.000 January 275.3275
2014-02-01 00:00:00.000 2014-02-31 00:00:00.000 February 275.3275
...
2015-01-01 00:00:00.000 2015-01-31 00:00:00.000 January 283.5875
2015-02-01 00:00:00.000 2015-02-31 00:00:00.000 February 283.5875
All the way through December for each yearly pay period.
稍后我将旋转“月份”列,将每月金额放在它们所属的相应月份下。
这是可行的,因为我在这一点上感到迷茫吗?