根据Teradata

时间:2015-08-18 04:53:48

标签: sql teradata

这更像是记录比较。如何在此处添加结束日期?请注意,最后一个日期来自tableB。示例:

TableA

PROD     | PRICE | EffDATE
----------------------------- 
Product1 | $15   | 03/17/2015  
Product1 | $20   | 03/20/2015  
Product1 | $21   | 03/22/2015

TableB

PROD     | PROMOSTART | PROMOEND
----------------------------------
Product1 | 03/17/2015 | 03/24/2015 

预期输出

 PROD     | PRICE | StartDATE  | EndDATE 
-----------------------------------------
Product1 | $15   | 03/17/2015  | 03/19/2015
Product1 | $20   | 03/20/2015  | 03/21/2015
Product1 | $21   | 03/22/2015  | **03/24/2015**

如何在 Teradata SQL 中实现这一目标?

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT a.*,
   COALESCE(MIN(EffDATE) -- next row's date
            OVER (PARTITION BY a.PROD
                  ORDER BY EffDATE
                  ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING) -1
           ,b.PROMOEND)  -- or end of promo
FROM tableA AS a
JOIN tableB AS b
  ON a.PROD = b.PROD
 AND a.EffDATE BETWEEN b.PROMOSTART AND b.PROMOEND