0

我有一张桌子 ProductPrice

INSERT INTO ProductPrice
(
ProductId,
EffectiveDt,
InactiveDt,
Price
)

我的日期之间有间隔或重叠

到目前为止我所拥有的表格按最早生效日期排名第一,然后是产品ID

这使用

SELECT 
    pp.ProductId,
    pp.EffectiveDt,
    pp.InactiveDt, 
    ROW_NUMBER() OVER (PARTITION BY pp.ProductId ORDER BY pp.EffectiveDt ASC) AS         RankByDate
INTO
    #ProductrankByDate
FROM 
    ProductPrice pp
ORDER BY 
    pp.ProductId

现在我想做的是更新

是否需要 InactiveDt 并使用下一个等级有效Dt 更新它

所以

01/07/2012 , 10/07/2012
15/07/2012 , 20/07/2012

01/07/2012 , 10/07/2012
08/07/2012 , 20/07/2012

会成为

01/07/2012 , 14/07/2012
15/07/2012 , 20/07/2012

01/07/2012 , 07/07/2012
08/07/2012 , 20/07/2012

我到目前为止

UPDATE pp
SET 
    pp.InactiveDt = DATEADD(DAY, -1, pdb.EffectiveDt)
FROM 
    #ProductrankByDate AS pp
INNER JOIN 
    #ProductrankByDate AS pdb 
ON 
    pp.ProductId  = pdb.ProductId
WHERE
    pp.RankByDate = pdb.RankByDate + 1
AND pp.ProductId  = pdb.ProductId 

但显然这不起作用,所以修改这个或新语句的任何帮助都会有很多帮助谢谢

4

1 回答 1

0

我设法让它工作我所要做的就是将 +1 移到 pp.RankByDate 后面而不是 pbd.RankByDate

UPDATE pp
SET 
    pp.InactiveDt = DATEADD(DAY, -1, pdb.EffectiveDt)
FROM 
    #ProductrankByDate AS pp
INNER JOIN 
    #ProductrankByDate AS pdb 
ON 
    pp.ProductId      = pdb.ProductId
WHERE
    pp.RankByDate + 1 = pdb.RankByDate
AND pp.ProductId      = pdb.ProductId 
于 2012-07-30T10:34:57.133 回答