Sql server 基于sql server中的上一行修改当前行
我有这样一个结果集:Sql server 基于sql server中的上一行修改当前行,sql-server,Sql Server,我有这样一个结果集: YearMonth Sales 201411 100 201412 100 201501 100 201502 100 201503 100 201504 100 201505 100 201506 100 201507 100 201508 100 需要再添加一行,销售额比上个月多4%。例如,我的结果应该是 YearMonth Sales New Sales 201411 10
YearMonth Sales
201411 100
201412 100
201501 100
201502 100
201503 100
201504 100
201505 100
201506 100
201507 100
201508 100
需要再添加一行,销售额比上个月多4%。例如,我的结果应该是
YearMonth Sales New Sales
201411 100 100.00
201412 100 104.00
201501 100 108.16
201502 100 112.49
201503 100 116.99
201504 100 121.67
201505 100 126.53
201506 100 131.59
201507 100 136.86
201508 100 142.33
请帮我找到最好的方法。是的,这是可能的。但首先您必须更改表并添加额外的列NewSales,然后尝试使用此链接 我想你可以通过这个链接来完成 sql server还支持sql server中具有持久值的某些计算列
使用该选项,您可以指定所需的公式,然后将根据您的公式自动创建新的列值,从而获得符合您要求的完美答案。花了很长时间才弄明白。只需使用表名更改临时表名,并验证列名
DECLARE @nCurrentSale FLOAT
DECLARE @nYeatDate INT
DECLARE @nSale FLOAT
CREATE TABLE #TempNEW(YearMonth VARCHAR(10), Sales FLOAT, NewSale FLOAT)
SELECT TOP 1 @nCurrentSale = Sales FROM #Temp
ORDER BY (CAST('01/' + SUBSTRING (CAST(YearMonth AS VARCHAR), 5 , 2) + '/' + SUBSTRING (CAST(YearMonth AS
VARCHAR), 0 , 5) AS DATETIME)) ASC
DECLARE Cursor1 CURSOR FOR
SELECT YearMonth, Sales FROM #Temp
ORDER BY (CAST('01/' + SUBSTRING (CAST(YearMonth AS VARCHAR), 5 , 2) + '/' + SUBSTRING (CAST(YearMonth AS
VARCHAR), 0 , 5) AS DATETIME)) ASC
OPEN Cursor1
FETCH NEXT FROM Cursor1 INTO @nYeatDate, @nSale
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO #TempNEW(YearMonth, Sales, NewSale) VALUES(@nYeatDate, @nSale, CAST(@nCurrentSale AS DECIMAL(12,2)))
SET @nCurrentSale = @nCurrentSale + ((@nCurrentSale/100) * 4)
FETCH NEXT FROM Cursor1 INTO @nYeatDate, @nSale
END
CLOSE Cursor1
DEALLOCATE Cursor1
SELECT * FROM #TempNEW
告诉我你的状态。这里有两个想法。。。如果我理解了用例,就不太清楚了。。。此外,此解决方案仅适用于SQL 2012及更高版本 这张桌子
CREATE TABLE [dbo].[LagExample](
[YearMonth] [nvarchar](100) NOT NULL,
[Sales] [money] NOT NULL
)
第一个是相当简单的,只是假设你想把你的百分比增长的幅度建立在它之前多少天的基础上
;WITH cte
as
(
SELECT YearMonth,
ROW_NUMBER() OVER (ORDER BY YearMonth) - 1 AS SalesEntry,
cast(LAG(Sales, 1,Sales) OVER (ORDER BY YearMonth) as float) as Sales
FROM LagExample
)
SELECT YearMonth,
Sales,
cast(Sales * POWER(cast(1.04 as float), SalesEntry) AS decimal(10,2)) as NewSales
FROM cte
第二种方法使用递归CTE来计算月份移动时的值。。
这里有一个关于递归CTE的好链接
列YearMonth的数据类型是什么?字符还是整数?年和月总是连续的吗?如果没有,如何计算差距后的下一个销售额?2015年7月的销售额为100,因此2015年8月的下一个月的销售额应为104,而不是142.33。所有值在100的基础上增加4%将为104。您没有提到计算新的销售价值。是否创建新行或新列?你已经有了NewSales专栏?YearMonth是Varchar。YearMonth总是连续的。非常感谢Veera。实际上,我的初始数据集不是一个表,而是从查询和许多其他字段中获取的。我现在正在做。再次感谢!!在Server2012中,我们还有其他方法可以做吗?因为我的数据集包含1000万行,使用游标可能会很慢。我试图弄清楚这一点
;with data
as
(
SELECT Lead(le.YearMonth, 1, null) OVER (ORDER BY le.YearMonth) as NextYearMonth,
cast(le.Sales as Decimal(10,4)) as Sales,
le.YearMonth
FROM LagExample le
)
,cte
as
(
SELECT *
FROM data
Where YearMonth = '201411'
UNION ALL
SELECT
data.NextYearMonth,
cast(cte.Sales * 1.04 as Decimal(10,4)) as Sales,
data.YearMonth
From cte join
data on data.YearMonth = cte.NextYearMonth
)
SELECT YearMonth, cast(Sales as Decimal(10,2))
FROM cte
order by YearMonth