SQL在条件下运行完全重置

SQL在条件下运行完全重置,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我有下表: TransactionHistoryId ProductCode Type Quantity PurchasePrice QtyRunning PriceRunning 1 Product1 B 10 3.00 10 30.00 2 Product1 B 5 7

我有下表:

TransactionHistoryId    ProductCode Type    Quantity    PurchasePrice   QtyRunning  PriceRunning
1                       Product1    B       10          3.00            10          30.00
2                       Product1    B       5           7.00            15          65.00
3                       Product1    S       -7          7.00            8           65.00
4                       Product1    S       -8          3.00            0           0.00
5                       Product1    B       4           10.00           4           40.00
6                       Product1    B       5           12.00           9           100.00
8                       Product2    B       8           20.00           8           160.00
事务历史记录表

我想填写下表:

TransactionHistoryId    ProductCode Type    Quantity    PurchasePrice   QtyRunning  PriceRunning
1                       Product1    B       10          3.00            10          30.00
2                       Product1    B       5           7.00            15          65.00
3                       Product1    S       -7          7.00            8           65.00
4                       Product1    S       -8          3.00            0           0.00
5                       Product1    B       4           10.00           4           40.00
6                       Product1    B       5           12.00           9           100.00
8                       Product2    B       8           20.00           8           160.00
创建表SQL

规则

PriceRunningTotal在运行总量为0时重置 PriceRunningTotal仅对类型为“B”的购买进行求和,当类型为“S”已售出时,保持上一次购买价格的总和 注意,有一个产品2,所以它应该有自己的运行计数,独立于产品1 目的

查询以最终查找以下内容:

Product     Quantity       AdjustedPurchasePrice
Product1    9              $11.11
Product2    8              $20 
我使用了以下SQL Server 2012查询来获得结果,但我觉得可以做得更好:

质疑

问题

理想情况下,我希望在另一个查询中使用QuantityRunningTotal,但我不能嵌套窗口函数


有谁知道更有效的方法来达到这个效果吗?

嗯。我想是这样的:

select th.*,
       sum(case when type = 'B' then Quantity * PurchasePrice
                else 0
           end) over (partition by grp, ProductCode order by TransactionHistoryId
                     ) as PriceRunningTotal
from (select th.*,
             sum(case when running_quantity = 0 then 1 else 0 end) over (partition by ProductCode order by TransactionHistoryId) as grp
      from (select th.*,
                   sum(quantity) over (partition by ProductCode order by TransactionHistoryId
                                      ) as running_quantity
            from TransactionHistory th
           ) th;
我不确定这是否与您的查询逻辑相同。对于此查询:

最里面的子查询计算运行数量。 中间子查询根据运行数量为0的次数计算组。 最外层的查询然后计算运行价格。
嗯,我想是这样的:

select th.*,
       sum(case when type = 'B' then Quantity * PurchasePrice
                else 0
           end) over (partition by grp, ProductCode order by TransactionHistoryId
                     ) as PriceRunningTotal
from (select th.*,
             sum(case when running_quantity = 0 then 1 else 0 end) over (partition by ProductCode order by TransactionHistoryId) as grp
      from (select th.*,
                   sum(quantity) over (partition by ProductCode order by TransactionHistoryId
                                      ) as running_quantity
            from TransactionHistory th
           ) th;
我不确定这是否与您的查询逻辑相同。对于此查询:

最里面的子查询计算运行数量。 中间子查询根据运行数量为0的次数计算组。 最外层的查询然后计算运行价格。
感谢您的努力,我运行了查询“我喜欢团队想法”,运行总数似乎正确,但交易历史后价格运行总数不会重置ID 5您可能需要滚动我的表格以查看完整的表格感谢您的努力,我运行了查询“我喜欢团队想法”,运行总计似乎正确,但在TransactionHistoryID 5之后运行总计的价格不会重置。您可能需要滚动我的表格以查看完整的表格。我不明白第三行中的价格是如何为65。你能解释一下吗?@Theesia是的,因为数量运行的总和不是零,它不是“B”类型,这意味着它已售出,那么我们只保留最后的总数,直到那一点。只有当QtyRunning中有0时,才会发生重置。我不明白第三行是如何运行65 as价格的。你能解释一下吗?@Theesia是的,因为数量运行的总和不是零,它不是“B”类型,这意味着它已售出,那么我们只保留最后的总数,直到那一点。只有当QtyRunning中存在0时,才会发生重置
select th.*,
       sum(case when type = 'B' then Quantity * PurchasePrice
                else 0
           end) over (partition by grp, ProductCode order by TransactionHistoryId
                     ) as PriceRunningTotal
from (select th.*,
             sum(case when running_quantity = 0 then 1 else 0 end) over (partition by ProductCode order by TransactionHistoryId) as grp
      from (select th.*,
                   sum(quantity) over (partition by ProductCode order by TransactionHistoryId
                                      ) as running_quantity
            from TransactionHistory th
           ) th;