0

我有一个这样的查询:

;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 ))

编辑

抱歉不清楚,“值”是 int 类型。例如,如果@inputdate = '20130708'' Value20130708' 的值是 30,而前一天 '20130707' 的 'Value' 是 20,那么它应该返回 (30 - 20),即 10。

4

2 回答 2

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
于 2013-07-09T10:07:38.500 回答
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是日期,收盘价是收盘价,我已经添加了一个额外的引导列用于表示目的。

于 2019-03-25T14:37:32.350 回答