单列数据中的差异

时间:2013-02-08 05:47:13

标签: sql oracle

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

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

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您可以使用LEAD分析函数

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

其它(多个)

答案 1 :(得分:1)

以下是使用CTERowNum的其他选项:

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 Fiddle

祝你好运。