使用当前行中的字段将增加应用到上一行

发布于 2024-10-27 01:26:24 字数 958 浏览 4 评论 0原文

我有一个像这样设置的临时表:

Type    Rate    TotalCost
----    ----    ---------
Type1   0.01    3276.00
Type2   0.01    3276.00
Type3   0.01    3276.00

但是,我需要获取当前行的速率,将其乘以之前的总计,然后将其添加到当前总计......以得出新的总计!

Type    Rate    TotalCost
----    ----    ---------
Type1   0.01    3276
Type2   0.01    3308.76
Type3   0.01    3341.85

所以 3276 的 1% 是 32.76。

3276 + 32.76 = 3308.76。

3308 的 1% 是 33.08。

3308.76 + 33.08 = 3341.85。

等等。

我尝试创建一个 CTE,就像这样

;with cte 
as 
(   
select Type, Rate, TotalCost, row_number() over (order by SortOrder asc) as RowNum   
from @Types
) 

select cur.RowNum, cur.Rate, prev.Rate, cur.TotalCost, isnull(prev.TotalCost * cur.Rate, cur.TotalCost) as NewTotal 
from cte cur 
left join cte prev on prev.RowNum = cur.RowNum + 1

......但它不起作用:(

任何人都可以帮忙吗?

我正在使用 SQL Server 2005!

提前致谢!

I have a temp table set up like this:

Type    Rate    TotalCost
----    ----    ---------
Type1   0.01    3276.00
Type2   0.01    3276.00
Type3   0.01    3276.00

However, I need to take the rate of the current row, times it by the previous total, then add it to the current total...to come up with the new total!

Type    Rate    TotalCost
----    ----    ---------
Type1   0.01    3276
Type2   0.01    3308.76
Type3   0.01    3341.85

So 1% of 3276 is 32.76.

3276 + 32.76 = 3308.76.

1% of 3308 is 33.08.

3308.76 + 33.08 = 3341.85.

And so on.

I tried creating a CTE, like so..

;with cte 
as 
(   
select Type, Rate, TotalCost, row_number() over (order by SortOrder asc) as RowNum   
from @Types
) 

select cur.RowNum, cur.Rate, prev.Rate, cur.TotalCost, isnull(prev.TotalCost * cur.Rate, cur.TotalCost) as NewTotal 
from cte cur 
left join cte prev on prev.RowNum = cur.RowNum + 1

...but it's not working :(

Can anyone help?

I'm using SQL Server 2005!

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苄①跕圉湢 2024-11-03 01:26:24

从您的示例结果来看,您似乎正在对所有以前的行进行求和,而不仅仅是最后一行。您可以在没有 CTE 的情况下做到这一点,例如:

declare @YourTable table (type varchar(15), rate float, TotalCost money)
insert @YourTable values
    ('Type1',   0.01,    3276.00),
    ('Type2',   0.01,    3276.00),
    ('Type3',   0.01,    3276.00);

select  cur.Type
,       case 
        when sum(prev.rate) is null then 0 
        else sum(prev.rate * prev.TotalCost) 
        end + cur.TotalCost
from    @YourTable cur
left join 
        @YourTable prev
on      prev.type < cur.type 
group by
        cur.type
,       cur.TotalCost

一个问题是示例数据中没有排序顺序。我在这里按类型排序;我希望您的真实表格中有更好的排序顺序!

另一个问题是只有第一行的 TotalCost 很重要。对于以下行,成本是根据第一行的成本和其他行的费率得出的。

From your example results, it looks like you're summing all previous rows, instead of just the last one. You can do that without a CTE, like:

declare @YourTable table (type varchar(15), rate float, TotalCost money)
insert @YourTable values
    ('Type1',   0.01,    3276.00),
    ('Type2',   0.01,    3276.00),
    ('Type3',   0.01,    3276.00);

select  cur.Type
,       case 
        when sum(prev.rate) is null then 0 
        else sum(prev.rate * prev.TotalCost) 
        end + cur.TotalCost
from    @YourTable cur
left join 
        @YourTable prev
on      prev.type < cur.type 
group by
        cur.type
,       cur.TotalCost

One problem is that there's no sort order in your example data. I'm sorting on type here; I'm hoping there's a better sort order in your real table!

Another problem is that only the TotalCost of the first row matters. For the following rows, the cost is derived from the first rows' cost, and other rows' rate.

夜无邪 2024-11-03 01:26:24

我发现最好的方法是编写一个循环:

DECLARE @Temp TABLE
(
    Id INT IDENTITY(1,1),
    Type VARCHAR(10),
    Rate FLOAT,
    TotalCost MONEY
)

INSERT INTO @Temp (Type, Rate, TotalCost)
VALUES ('Type1', 0.01, 3276.00),
       ('Type2', 0.01, 3276.00),
       ('Type3', 0.01, 3276.00)

DECLARE @CurrentId INT, 
        @Total INT,
        @PreviousCalc MONEY

SET @CurrentId = 1
SELECT @Total = MAX(Id) FROM @Temp

WHILE (@CurrentId <= @Total)
BEGIN
    SELECT @PreviousCalc = Rate * TotalCost
    FROM @Temp
    WHERE Id = @CurrentId - 1

    UPDATE @Temp
    SET TotalCost = TotalCost + ISNULL(@PreviousCalc, 0)
    WHERE Id = @CurrentId

    SET @CurrentId = @CurrentId + 1
END

SELECT * FROM @Temp

The best way I've found is to write a loop:

DECLARE @Temp TABLE
(
    Id INT IDENTITY(1,1),
    Type VARCHAR(10),
    Rate FLOAT,
    TotalCost MONEY
)

INSERT INTO @Temp (Type, Rate, TotalCost)
VALUES ('Type1', 0.01, 3276.00),
       ('Type2', 0.01, 3276.00),
       ('Type3', 0.01, 3276.00)

DECLARE @CurrentId INT, 
        @Total INT,
        @PreviousCalc MONEY

SET @CurrentId = 1
SELECT @Total = MAX(Id) FROM @Temp

WHILE (@CurrentId <= @Total)
BEGIN
    SELECT @PreviousCalc = Rate * TotalCost
    FROM @Temp
    WHERE Id = @CurrentId - 1

    UPDATE @Temp
    SET TotalCost = TotalCost + ISNULL(@PreviousCalc, 0)
    WHERE Id = @CurrentId

    SET @CurrentId = @CurrentId + 1
END

SELECT * FROM @Temp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文