表中跨多个日期的tsql数学

表中跨多个日期的tsql数学,tsql,Tsql,我有一个@variabletable,简单定义为EOMDate(日期时间)、DandA(浮动)、优惠券(浮动)、EarnedIncome(浮动) 我要做的是在填充表之后,我需要返回并计算EarnedIncome字段来填充它。 公式为当月的DandA减去上月的DandA加上息票。 我遇到的问题是如何进行更新?因此,对于6/30,该值应为4453.12(2375-24640.63)+26718.75 为了解决这个问题,我很乐意用棍棒打我的头。谢谢此外,在MS SQL2005下运行,以便在可能的情况下

我有一个@variabletable,简单定义为EOMDate(日期时间)、DandA(浮动)、优惠券(浮动)、EarnedIncome(浮动)

我要做的是在填充表之后,我需要返回并计算EarnedIncome字段来填充它。 公式为当月的DandA减去上月的DandA加上息票。 我遇到的问题是如何进行更新?因此,对于6/30,该值应为4453.12(2375-24640.63)+26718.75


为了解决这个问题,我很乐意用棍棒打我的头。谢谢此外,在MS SQL2005下运行,以便在可能的情况下可以使用任何CTE ROW_OVER type解决方案。

您需要使用如下子查询:

UPDATE @variabletable v1
SET EarnedIncome = DandA 
- (SELECT DandA FROM @variabletable v2 WHERE GetMonthOnly(DATEADD(mm, -1, v2.EOMDate)=GetMonthOnly(v1.EOMDate))
+ Coupon
我正在利用这个辅助函数

DROP FUNCTION GetMonthOnly
GO
CREATE FUNCTION GetMonthOnly
(
    @InputDate DATETIME 
)
RETURNS DATETIME
BEGIN
    RETURN CAST(CAST(YEAR(@InputDate) AS VARCHAR(4)) + '/' +
                CAST(MONTH(@InputDate) AS VARCHAR(2)) + '/01' AS DATETIME)
END
GO

您需要使用如下子查询:

UPDATE @variabletable v1
SET EarnedIncome = DandA 
- (SELECT DandA FROM @variabletable v2 WHERE GetMonthOnly(DATEADD(mm, -1, v2.EOMDate)=GetMonthOnly(v1.EOMDate))
+ Coupon
我正在利用这个辅助函数

DROP FUNCTION GetMonthOnly
GO
CREATE FUNCTION GetMonthOnly
(
    @InputDate DATETIME 
)
RETURNS DATETIME
BEGIN
    RETURN CAST(CAST(YEAR(@InputDate) AS VARCHAR(4)) + '/' +
                CAST(MONTH(@InputDate) AS VARCHAR(2)) + '/01' AS DATETIME)
END
GO

可能有一种方法可以在一条语句中实现这一点,但在这种情况下,我倾向于设置一个光标遍历每一行,计算该行的新EarnedIncome字段,更新该行,然后移动到下一行

例:


可能有一种方法可以在一条语句中实现这一点,但在这种情况下,我倾向于设置一个光标遍历每一行,计算该行的新EarnedIncome字段,更新该行,然后移动到下一行

例:


您可以使用子查询执行计算,唯一的问题是第一个月怎么办,因为没有以前的DADA值。在这里,我使用isnull将其设置为0。这个查询看起来像

Update MyTable
Set EarnedIncome = DandA + Coupon - IsNull(  Select Top 1 DandA 
                                             From MyTable2 
                                             Where MyTable.EOMDate > MyTable2.EOMDate 
                                             Order by MyTable2.EOMDate desc), 0)

这还假设每个表中每个月只有一条记录,并且月份之间没有任何间隔。

您可以使用子查询执行计算,唯一的问题是第一个月怎么办,因为没有以前的DADA值。在这里,我使用isnull将其设置为0。这个查询看起来像

Update MyTable
Set EarnedIncome = DandA + Coupon - IsNull(  Select Top 1 DandA 
                                             From MyTable2 
                                             Where MyTable.EOMDate > MyTable2.EOMDate 
                                             Order by MyTable2.EOMDate desc), 0)

这还假设每个表中每个月只有一条记录,并且月与月之间没有任何间隔。

另一种选择是在插入数据时计算运行总数,并有一个约束条件保证运行总数是正确的:


另一种选择是在插入数据时计算运行总数,并有一个约束条件保证您的运行总数是正确的:


有很多方法可以做到这一点。您将根据数据集的大小和其他因素找到利弊

这是我的建议

Declare @table as table 
(
    EOMDate DateTime, 
    DandA float,
    Coupon Float,
    EarnedIncome Float
)

Insert into @table Values('04/30/2008', 20187.5,17812.5,NULL)
Insert into @table Values('05/31/2008', 24640.63, 22265.63, NULL)
Insert into @table Values('06/30/2008', 2375, 26718.75,NULL)


--If we know that EOMDate will only contain one entry per month, and there's *always* one entry a month...
Update @Table Set
EarnedIncome=DandA-
(Select top 1 DandA 
from @table t2 
where t2.EOMDate<T1.EOMDate 
order by EOMDate Desc)+Coupon
From @table T1
Select * from @table

--If there's a chance that there could be more per month, or we only want the values from the previous month (do nothing if it doesn't exist)

Update @Table Set
EarnedIncome=DAndA-(
Select top 1 DandA
From @table T2
Where DateDiff(month, T1.EOMDate, T2.EOMDate)=-1
Order by EOMDate Desc)+Coupon
From @Table T1

Select * from @table
--Leave the null, it's good for the data (since technically you cannot calculate it without a prior month).
这完全是你到底想要什么的问题。 使用记录直接处理当前记录(无论日期),或仅使用当前记录前一个月的记录

对不起,格式。。。Stackoverflow.com的答案编辑和我在一起玩得不好


:D

确实有很多方法可以做到这一点。您将根据数据集的大小和其他因素找到利弊

这是我的建议

Declare @table as table 
(
    EOMDate DateTime, 
    DandA float,
    Coupon Float,
    EarnedIncome Float
)

Insert into @table Values('04/30/2008', 20187.5,17812.5,NULL)
Insert into @table Values('05/31/2008', 24640.63, 22265.63, NULL)
Insert into @table Values('06/30/2008', 2375, 26718.75,NULL)


--If we know that EOMDate will only contain one entry per month, and there's *always* one entry a month...
Update @Table Set
EarnedIncome=DandA-
(Select top 1 DandA 
from @table t2 
where t2.EOMDate<T1.EOMDate 
order by EOMDate Desc)+Coupon
From @table T1
Select * from @table

--If there's a chance that there could be more per month, or we only want the values from the previous month (do nothing if it doesn't exist)

Update @Table Set
EarnedIncome=DAndA-(
Select top 1 DandA
From @table T2
Where DateDiff(month, T1.EOMDate, T2.EOMDate)=-1
Order by EOMDate Desc)+Coupon
From @Table T1

Select * from @table
--Leave the null, it's good for the data (since technically you cannot calculate it without a prior month).
这完全是你到底想要什么的问题。 使用记录直接处理当前记录(无论日期),或仅使用当前记录前一个月的记录

对不起,格式。。。Stackoverflow.com的答案编辑和我在一起玩得不好


:D

已经有了一个相等日期函数,但这个答案让我找到了正确的方向,谢谢!已经有一个相等的日期函数,但这个答案让我指向了正确的方向,谢谢!