Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从包含累积和增量项的表中查找滞后累积总计_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 从包含累积和增量项的表中查找滞后累积总计

Sql 从包含累积和增量项的表中查找滞后累积总计,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有一个带有模式的SQL表,其中的值要么是特定类别的累积值,要么是前一个值之上的增量。虽然我知道这不是一个特别好的设计,但它来自外部,因此我不能以任何方式改变它 该表如下所示: Date Category AmountSoldType AmountSold ----------------------------------------------------- Jan 1 Apples Cumulative 100 Jan

我有一个带有模式的SQL表,其中的值要么是特定类别的累积值,要么是前一个值之上的增量。虽然我知道这不是一个特别好的设计,但它来自外部,因此我不能以任何方式改变它

该表如下所示:

Date Category AmountSoldType AmountSold ----------------------------------------------------- Jan 1 Apples Cumulative 100 Jan 1 Bananas Cumulative 50 Jan 2 Apples Delta 20 Jan 2 Bananas Delta 10 Jan 3 Apples Delta 25 Jan 3 Bananas Cumulative 75
是我想要的,但是我在组合正确的子查询时遇到了问题。有什么建议吗?

您似乎想将增量添加到最近的累计值中——所有增量都在当前日期之前

如果是这样,我认为这一逻辑符合您的要求:

select f.*,
       (max(case when date = date_cumulative then amountsold else 0 end) over (partition by category
           ) +
        sum(case when date > date_cumulative then amountsold else 0 end) over (partition by category order by date rows between unbounded preceding and 1 preceding
           )
       ) amt
from (select f.*,
             max(case when AmountSoldType = 'cumulative' then date else 0 end) over
                 (partition by category order by date rows between unbounded preceding and current_row
                 ) as date_cumulative
      from fruits f
     ) f

我对这个数据集有点困惑(尽管把苹果加起来是错误的)。我假设原始数据显示了一天结束时的数据,例如1月2日售出了20个苹果(因为当天报告的增量为20个)

在您的示例结果中,说1月1日零苹果售出似乎是无效的。实际上不可能说出当天卖出了多少,因为不清楚这100个累积苹果是在1月1日期间累积的(因此应该从您寻求的日初数字中排除),还是在前几天累积的(应该包括在内),或者两者的混合。因此,当天的数据应该是空的

还不清楚是否所有数据集都必须以累积开始,或者数据集是否可以以增量开始(可能需要从后续累积开始向后工作),以及您是否有可能从外部源访问多个数据集,这些数据集形成连续一致的序列,或者是否为“累积”仅与接收到的单个数据集相关。我将假设至少所有的数据集都是以一个累积值开始的

尽管如此,这个问题是一个简单的情况,首先将所有行转换为所有增量或所有累积量。假设我们使用所有累积数,然后按顺序递归每一行,则可以按原样选择
amountsell
(如果该行是累积的),或者将amountsell添加到上一步的结果中(如果是增量)

一旦像这样进行了预处理,那么对于一天的开始累积,就只需要查看前一天的累积(这是一天的结束累积,如果我最初的假设是正确的,所有原始数据都与一天的结束数字相关)

在最后一步中使用LAG函数来获取前一天的累计值,也将为第一行生成一个空值

SELECT Date, Category
       LEAD((subquery??), 1) OVER (PARTITION BY Category ORDER BY Date) AS Amt
FROM Fruits
GROUP BY Date, Category
ORDER BY Date ASC
select f.*,
       (max(case when date = date_cumulative then amountsold else 0 end) over (partition by category
           ) +
        sum(case when date > date_cumulative then amountsold else 0 end) over (partition by category order by date rows between unbounded preceding and 1 preceding
           )
       ) amt
from (select f.*,
             max(case when AmountSoldType = 'cumulative' then date else 0 end) over
                 (partition by category order by date rows between unbounded preceding and current_row
                 ) as date_cumulative
      from fruits f
     ) f