[英]Using LEAD Function and do deduct calculation in one SQL query
Having Lead function and doing calculation(deduct) in ONE select statement instead of using nested select statement to achieve the results 具有Lead函数并在一个select语句中执行计算(扣除),而不是使用嵌套的select语句来获得结果
Below shows my current not working sql statement. 下面显示了我当前无法正常工作的sql语句。 CurrentDate - Nxcurrentdate and having a column with showing the difference:
CurrentDate-Nxcurrentdate,并具有显示差异的列:
SELECT
ID,
CURRENTDATE,
LEAD(CURRENTDATE,1) OVER ( PARTITION BY ID ORDER BY ID, CURRENTDATE ) NX_DATE,
NX_DATE - CURRENTDATE AS DATE_DIFF
FROM TABLEA
Expected results should be: 预期结果应为:
ID CurrentDate NxDate DateDiff
you can use datediff(interval, date1, date2)
and getdate()
您可以使用
datediff(interval, date1, date2)
和getdate()
Select *, DATEDIFF(day, t1.CURRENTDATE, t1.NX_DATE) AS DATE_DIFF) from (
SELECT
ID, getdate() as CURRENTDATE,
LEAD(getdate(),1) OVER ( PARTITION BY ID ORDER BY ID, getdate()) NX_DATE
FROM TABLEA) t1
You can try below: 您可以尝试以下操作:
SELECT
ID, CURRENTDATE,
LEAD(CURRENTDATE,1) OVER ( PARTITION BY ID ORDER BY ID, CURRENTDATE ) NX_DATE,
datediff(day,CURRENTDATE,LEAD(CURRENTDATE,1) OVER ( PARTITION BY ID ORDER BY ID, CURRENTDATE)) AS DATE_DIFF
from tablename
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.