使用WITH语句的结果更新临时表

时间:2015-09-07 09:14:59

标签: sql-server tsql

使用SQL Server 2008 R2:我无法理解我使用WITH语句的结果更新临时表。

临时表,它将是我的主表,让我们称之为#main,这很简单,看起来就像它的第一行(将有数百个产品代码,所以我不能努力在解决方案中编写产品代码):

Product |  GapForGoodsinWH1  |  GapForGoodsinWH2
1000    |           NULL     |            NULL

在我使用WITH语句运行之前,NULLS就在那里。 WITH声明的目的是确定货物尚未收到某个仓库的最长时间(以天为单位)(在此示例中表示为WH1和WH2。

如果我识别产品代码和仓库,我的WITH语句可以正常工作:

WITH GoodsIn AS (
    SELECT 
        Product,
        ROW_NUMBER() OVER (ORDER BY GoodsInDate DESC) AS RN,
        GoodsInDate
    FROM GoodsIn
    WHERE 
    GoodsInDate >= @StartDate
    and GoodsInDate <= @EndDate
    --
    AND Product = 1000
    AND Warehouse = 'WH1'
)
SELECT
    A.Product,
    MAX(DATEDIFF(DD, A.GoodsInDate, B.GoodsInDate)) AS TimeMissing
FROM 
    GoodsIn A
LEFT JOIN 
    GoodsIn B ON A.RN = B.RN + 1
GROUP BY   
    A.Product
HAVING 
    MAX(DATEDIFF(DD, A.GoodsInDate, B.GoodsInDate)) >= @Missing

因此,日期参数会查看某个时间段,@Missing参数是供用户列出产品的,如果它们在X时间段内缺失(在参数中定义)。

我想要做的就是将TimeMissing值导出到我的#main表中,方法是在Product上更新它。在此示例中,我希望通过将产品与#main.GapForGoodsinWH1#main.productWarehouse相匹配来更新#main.GapForGoodsinWH1

欢迎任何想法,提前谢谢。

3 个答案:

答案 0 :(得分:1)

我不知道这是否是您想要的结果,但请告诉我是否与您想要的不同:

WITH GoodsIn AS(
    SELECT 
        Product,
        ROW_NUMBER() OVER (PARTITION BY Product ORDER BY Product, GoodsInDate DESC) AS RN,
        GoodsInDate,
        WareHouse
    FROM GoodsIn
    WHERE 
    GoodsInDate >= @StartDate
    AND GoodsInDate <= @EndDate
    AND Product = @ID -- specidied product
    AND WareHouse = @WareHouse -- specidied WareHouse
)

Update m set GapForGoodsinWH1 = TBL.TimeMissing
From #main m
INNER JOIN 
(
    SELECT
        A.Product,
        MAX(DATEDIFF(DD, A.GoodsInDate, B.GoodsInDate)) AS [TimeMissing]
    FROM 
    GoodsIn A
    LEFT JOIN 
    GoodsIn B ON A.Product = B.Product AND A.RN = B.RN + 1
    GROUP BY   
    Product
)TBL ON TBL.Product = m.Product
 WHERE TBL.TimeMissing >= @Missing

答案 1 :(得分:0)

您可以使用lag窗口功能查找以前的货件:

lag(GoodsInDate) over (
    partition by Product, Warehouse 
    order by GoodsInDate)

然后,您可以查找每个产品的每个产品的最大日期差异,并将其存储在main中:

; with  GoodsInWithDelta as
        (
        select  Product
        ,       GoodsInDate
        ,       lag(GoodsInDate) over (
                    partition by Product
                    order by GoodsInDate) as PrevDate
        from    GoodsIn
        where   Warehouse = 'WH1'
        )
,       MaxDeltaByProduct as
        (
        select  Product
        ,       max(datediff(day, PrevDate, GoodsInDate)) as MaxDelta
        from    GoodsInWithDelta
        group by
                Product
        )
update  m
set     GapForGoodsinWH1 = MaxDelta
from    main m
left join
        MaxDeltaByProduct mdbp
on      m.Product = mdbp.Product

Example at SQLFiddle.您可以对第二个仓库重复此查询。

答案 2 :(得分:-1)

这应该有效:

WITH GoodsIn AS (
    SELECT 
        Product,
        ROW_NUMBER() OVER (ORDER BY GoodsInDate DESC) AS RN,
        GoodsIn
    FROM GoodsIn
    WHERE 
    GoodsIn >= @StartDate
    and GoodsIn <= @EndDate
    --
    AND Product = 1000
    AND Warehouse = 'WH1'
)
Update m set GapForGoodsinWH1 = g.TimeMissing
From #main m
Inner Join (
    SELECT
        A.Product,
        MAX(DATEDIFF(DD, A.GoodsInDate, B.GoodsInDate)) AS TimeMissing
    FROM GoodsIn A
    LEFT JOIN GoodsIn B
        ON A.RN = B.RN + 1
    GROUP BY   A.Product
    HAVING MAX(DATEDIFF(DD, A.GoodsInDate, B.GoodsInDate)) >= @Missing
) as g 
    on g.product = m.product