rows between 1 preceding and preceding 1

空扰寡人 提交于 2020-01-06 06:06:09

问题


I am new to SQL and I want to know what exactly the function ( rows between 1 preceding and 1 preceding )do in teradata ,I want a simple clarification please ,I am trying to use this function as a testcase to get the time gaps in history table between start and end date,can anyone help please or provide any useful links.

SELECT DISTINCT CUST_ID
FROM
(
SELECT 
CUST_ID,
STRT_dt - 
MIN(END_dt) OVER (PARTITION BY CUST_ID ORDER BY END_dt
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) AS diff
FROM table
QUALIFY diff > 1
) dt

回答1:


This returns the same result as Standard SQL's LAG(END_dt) OVER (PARTITION BY CUST_ID ORDER BY END_dt, i.e. the previous row's END_dt (or NULL for the 1st row per CUST_ID).

When you switch to FOLLOWING instead of PRECEDING it's the next row, LEAD in Standard SQL.

Both LAG and LEAD are finally implemented in TD16.10.

As you simply want to find gaps and you don't access the actual difference you can also simplify it to:

SELECT DISTINCT CUST_ID
FROM table
QUALIFY
   STRT_dt - 
   MIN(END_dt)
   OVER (PARTITION BY CUST_ID
         ORDER BY END_dt
         ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING) > 1


来源:https://stackoverflow.com/questions/48784722/rows-between-1-preceding-and-preceding-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!