我有这样的查询:
;WITH A AS (SELECT * FROM T1 where T1.targetDate=@inputdate),
B AS (SELECT A.*, T2.SId, T2.Type, T2.Value
FROM A
INNER JOIN T2 ON A.SId = T2.SId )
SELECT A.*, B.Type, B.Value
FROM B
我的问题是,不是Value
@inputdate,
Value
如何在@inputdate
和前一天(DATEADD(day, -1, @inputdate ))
之间获得@inputdate = '20130708'
的增量?
被修改:
很抱歉不清楚,'Value'是int类型。例如,如果Value
,'20130708'的{{1}}为30,前一天'20130707'的'值'为20,则返回(30 - 20)为10。
答案 0 :(得分:0)
像这样的东西,假设Value是DATE格式
;WITH A AS (SELECT * FROM T1 where T1.targetDate=@inputdate),
B AS (SELECT A.*, T2.SId, T2.Type, T2.Value
FROM A
INNER JOIN T2 on A.SId = T2.SId )
SELECT A.*, T2.Type, T2.Value, DATEDIFF(DAY, b.Value, DATEDADD(DAY, -1,@InputDate)) AS Delta
FROM B
答案 1 :(得分:0)
假设您有一个股票价格表:其中包含代码,日期和收盘价等,您可以使用以下代码:
select symb, ret_dt, close, (close-(lead(close,1) over (partition by symb order by ret_dt desc,close)))as difference, (lead(close,1) over
(partition by symb order by ret_dt desc,close)) as lead
from stocks.nyse2010;
注意::此处ret_dt
是日期,收盘价是收盘价,为了说明性目的,我添加了一个额外的潜在客户栏。