Sql server 从库存表中,找出库存为零的两个日期之间的日期

Sql server 从库存表中,找出库存为零的两个日期之间的日期,sql-server,Sql Server,我有一个SQL表,其中有数千条关于产品流的记录,如果产品售出,数量为负,购买时数量为正产品id是每个产品的唯一标识符。我想输入日期并找出这两个日期之间的每个月,什么时候库存为零 我想首先计算每个月的起始余额,只需找出过去期间的总和,然后逐行添加新数据,如果它为零,则为日期,但这种逻辑似乎非常糟糕,甚至不知道如何在SQL中处理它 我使用微软SQL 2014 declare @Table table (general_id bigint, date datetime, product_id bigi

我有一个SQL表,其中有数千条关于产品流的记录,如果产品售出,数量为负,购买时数量为正<代码>产品id是每个产品的唯一标识符。我想输入日期并找出这两个日期之间的每个月,什么时候库存为零

我想首先计算每个月的起始余额,只需找出过去期间的总和,然后逐行添加新数据,如果它为零,则为日期,但这种逻辑似乎非常糟糕,甚至不知道如何在SQL中处理它

我使用微软SQL 2014

declare @Table table (general_id bigint, date datetime, product_id bigint, quantity float, 
price float, code nvarchar(max), name nvarchar(max), partnumber nvarchar(max), 
description nvarchar(max), rate float, comment nvarchar(max), currency nvarchar(max), waybill nvarchar(max))

insert into @Table (general_id, date, product_id, quantity, price, code, name, partnumber, description, rate, comment, currency, waybill)
  select 1, '2019-03-1 16:33:00', 1, 10, 100, 101010, 'test', 'testnumber', 'testdescription', 1.0, 'testcomment', 'USD', 'nobill'
  union all
  select 2, '2019-03-2 16:33:09', 1, -5, 100, 101010, 'test', 'testnumber', 'testdescription', 1.0, 'testcomment', 'USD', 'nobill'
  union all
  select 3, '2019-03-3 16:33:12', 1, -3, 100, 101010, 'test', 'testnumber', 'testdescription', 1.0, 'testcomment', 'USD', 'nobill'
  union all
  select 4, '2019-03-4 16:39:00', 1, -2, 100, 101010, 'test', 'testnumber', 'testdescription', 1.0, 'testcomment', 'USD', 'nobill'
  union all
  select 5, '2019-03-4 16:39:41', 2, 40, 100, 102020, 'test', 'testnumber', 'testdescription', 1.0, 'testcomment', 'USD', 'nobill'
  union all
  select 6, '2019-03-5 16:39:00', 2, -40, 100, 202020, 'test', 'testnumber', 'testdescription', 1.0, 'testcomment', 'USD', 'nobill'
  union all
  select 7, '2019-03-6 16:39:00', 1, 25, 100, 101010, 'test', 'testnumber', 'testdescription', 1.0, 'testcomment', 'USD', 'nobill'


SELECT DISTINCT product_id, code, name, partnumber, SUM(quantity) 
FROM @TABLE
GROUP BY product_id, code, name, partnumber 
ORDER BY product_id ASC                                 

如果输入的日期范围为2019-03-01至2019-03-31,则当前情况的输出为。 产品编号:1 缺货日期:2019-03-4 零库存天数:2天,因为在2019-03-6年,商品已被购买并已在库存中

产品编号:2 缺货日期:2019-03-5
零库存天数:26天,因为它再也没有被购买过

请使用Sql Server>2008中提供的窗口函数尝试此查询:)


请显示预期结果。我检查了你在那里做的事情,显然有些怀疑你是否正确理解了所有事情,因为这看起来很奇怪,检查了你的个人资料,似乎你应该理解。最后我成功地完成了所有的事情。我仍然不明白这是什么,但它是美妙的,也是美丽的,有点害羞,但华丽。谢谢你,伙计。我非常感激。对结果的感觉和看到它100%的正确性,是一个令人惊讶的过程
SELECT [lagDate] outOfStockStart,
       [date] outOfStockEnd, 
       product_id, 
       daysOutOfStock
FROM (
    SELECT *, 
           CASE WHEN LAG(stock) OVER (PARTITION BY product_id ORDER BY [date]) = 0 AND stock > 0 THEN
             DATEDIFF(day, lagDate, [date]) ELSE NULL END daysOutOfStock
    FROM (
        SELECT [date],
               LAG([date]) OVER (PARTITION BY product_id ORDER BY [date]) lagDate,
               product_id,
               SUM(quantity) over (PARTITION BY product_id ORDER BY [date]) stock
        FROM @TABLE
    ) a
) a WHERE daysOutOfStock IS NOT NULL