SQL oracle 的超前和滞后功能的 Teradata 等价物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8124756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Teradata equivalent for lead and lag function of oracle
提问by user708477
I have been working ot see the equivalent function for Oracle lead and lag function.
我一直在努力寻找 Oracle 超前和滞后功能的等效功能。
The oracle lead would look like
oracle 领导看起来像
LEAD(col1.date,1,ADD_MONTHS(col1.DATE,12))
OVER(Partition By tab.a,tab.b,tab.c Order By tab.a)-1 END_DATE
LAG(col1.DATE + 7,1,col1.DATE-1)
OVER(partition by tab.a,tab.b Order By tab.b) LAG_DATE
Any better idea
任何更好的主意
回答by Rob Paller
I believe you can take the following SQL as a basis and modify it to meet your needs:
相信你可以以下面的SQL为基础,进行修改以满足你的需求:
SELECT CALENDAR_DATE
, MAX(CALENDAR_DATE)
OVER(PARTITION BY 1 ORDER BY CALENDAR_DATE
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS Lag_ --Yesterday
, MIN(CALENDAR_DATE)
OVER(PARTITION BY 1 ORDER BY CALENDAR_DATE
ROWS BETWEEN 1 FOLLOWING AND 1 FOLLOWING) AS Lead_ --Tomorrow
FROM SysCalendar.CALENDAR
WHERE year_of_calendar = 2011
AND month_of_year = 11
NULL is returned when there is no record before or after and can be addressed with a COALESCE as necessary.
如果之前或之后没有记录,则返回 NULL,并且可以根据需要使用 COALESCE 进行寻址。
EDITIn Teradata 16.00 LAG/LEAD functions were introduced.
编辑在 Teradata 16.00 中引入了 LAG/LEAD 功能。