更新排名选择元组

时间:2012-07-30 10:04:57

标签: mysql date inner-join ranking

我有一张表ProductPrice

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

我的日期之间有差距 或重叠

到目前为止,我最早的生效日期排名为1,然后是产品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并使用下一个等级EffectiveDt

更新它

所以

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 

但显然这不起作用所以任何有关修改此声明或新声明的帮助都会有很多帮助,谢谢

1 个答案:

答案 0 :(得分:0)

我设法让它工作我所要做的就是将pp.RankByDate后面的+1移动而不是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