Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

sql - Teradata equivalent for lead and lag function of oracle

I have been working ot see the equivalent function for Oracle lead and lag function.

The oracle lead would look like

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe you can take the following SQL as a basis and modify it to meet your needs:

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.

EDIT In Teradata 16.00 LAG/LEAD functions were introduced.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...