简体   繁体   English

从列中的前一个值中减去与当前行中其他值的差异

[英]Subtract the difference from the others in the current row from the previous value in the column

Good afternoon: I want to get the following result.下午好:我想得到以下结果。 to subtract the remainder of the difference that must be shipped.减去必须运送的差额的剩余部分。 I try through the LAG function, It turns out.我尝试通过 LAG function,结果是。 but somehow everything is crooked.但不知何故,一切都是歪的。 Tell me how you can write it in SQL more elegantly.告诉我如何更优雅地写在 SQL 中。

在此处输入图像描述

CREATE TABLE TestTable(
[id] INT IDENTITY,
[productid] INT,
[name] NVARCHAR(256),
[ordered] DECIMAL(6,3),
[delivered] DECIMAL(6,3),
[remainder] DECIMAL(6,3));

INSERT INTO TestTable ([productid], [name], [ordered], [delivered], [remainder])
VALUES (712054, 'Product OSFNS', 253, 246.005, 13.255),
        (712054, 'Product OSFNS', 186, 183.63, 13.255),
        (712054, 'Product OSFNS', 196.8, 193.745, 13.255),
        (712054, 'Product OSFNS', 480, 477.025, 13.255)

And the query:和查询:

WITH CTE_diff AS
(SELECT 
     T1.[id]
    ,T1.[productid]
    ,T1.[name]
    ,T1.[ordered]
    ,T1.[delivered]
    ,T1.[remainder]
    ,LAG(T2.[ordered] - T2.[delivered], 1, T1.[ordered] - T1.[delivered]) 
        OVER (ORDER BY T2.[productid])  as R
FROM TestTable T1 JOIN TestTable T2
    ON T1.id = T2.id - 1

UNION 

SELECT *
FROM (
    SELECT TOP(1)
         T1.[id]
        ,T1.[productid]
        ,T1.[name]
        ,T1.[ordered]
        ,T1.[delivered]
        ,T1.[remainder]
        ,LEAD(T2.[ordered] - T2.[delivered], 1, T1.[ordered] - T1.[delivered]) 
            OVER (ORDER BY T2.[productid]) as R
    FROM TestTable T1 JOIN TestTable T2
        ON T1.id = T2.id
    ORDER BY T1.id DESC
) as tbl)

SELECT * FROM CTE_diff; 

My best guess is that you want cumulative sums:我最好的猜测是你想要累积总和:

select tt.*,
       remainder + sum(delivered - ordered) over (partition by productid order by id) as net_amount
from testtable tt;

Here is a db<>fiddle. 是一个 db<>fiddle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从上一行减去当前行,保持上一行值不变 — SQL - Subtract current row from previous row, keeping previous row value as constant — SQL 从上一行减去与分组有关的值 - Subtract a value from previous row respecting the grouping 检索上一行以按日期从当前行中减去 - Retrieve previous row to subtract from current row by date 将前一行值减去当前行 - Subtract previous row value to current row 从前一小时获取值并从 SQL 中的当前值中减去 - Get value from previous hour and subtract from current value in SQL 如何使用 LAG function 从当前记录中减去最后一条记录并将此值放入数据库增长的新列中 - How to use the LAG function to subtract the last previous record from current and place this value in a new column for Database Growth 当不存在ID时,SQL从记录值中减去上一行 - SQL subtract previous row from record value when no ID exists 将前一行值减去当前行并按日期排序 - Subtract previous row value to current row and order by date 如何在Hive的组中从当前行中减去前一行? - How do I subtract the previous row from the current row within a group in Hive? 如果前一行的列值是相同的雪花,则从表中删除当前行 - Removing the current row from a table if the column value of the previous row is the same Snowflake
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM