Sql 单列数据中的差异

Sql 单列数据中的差异,sql,oracle,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) 我想我可以创建另外两列Date days减-1和amount,然

我试图找出单列数据中的差异,即

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)
我想我可以创建另外两列Date days减-1和amount,然后显示差值

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
A列B列C列
1/1/12            550             -150
1/2/12            400              300
1/3/12            700             -200
1/4/12            500

感谢您的帮助

您可以使用
LEAD
分析功能

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


    • 这里是另一个使用
      CTE
      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
      
      还有


      祝你好运。

      @sgedes有时这些功能会被忽略。