2

我试图找到单列数据的差异,即不同的是

Column C
date 2 (1/2/12) would check the difference from date 1 (1/1/12)
date 3 (1/3/12) would check the difference from date 2 (1/2/12)
date 4 (1/4/12) would check the difference from date 3 (1/3/12)

我想我可以创建另外两列日期天数减去 -1 和金额然后显示差异

Column A<date>  Column B<Amount> Column C <Difference>
1/1/12            550             -150
1/2/12            400              300
1/3/12            700             -200
1/4/12            500

谢谢你的帮助

4

2 回答 2

2

你可以使用LEAD解析函数

SELECT  "Column A", 
        "Column B",
        (LEAD("Column B", 1) OVER (ORDER BY "Column A") - "Column B") AS "Difference"
FROM    TableName

其他)

于 2013-02-08T05:55:11.103 回答
1

这是使用CTEand的另一个选项RowNum

WITH CTE AS (
  SELECT  ColA,
        ColB,
        rownum rn
  FROM    YourTable
  ORDER BY ColA
)
SELECT C.*,
  C.ColB - C2.ColB ColC
FROM CTE C
  LEFT JOIN CTE C2 ON C.rn = C2.rn + 1

还有SQL 小提琴

祝你好运。

于 2013-02-08T05:57:45.400 回答