gpt4 book ai didi

sql - 单列数据差异

转载 作者:行者123 更新时间:2023-12-04 05:12:18 25 4
gpt4 key购买 nike

我试图找到单列数据中的差异,即差异将是

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

谢谢你的帮助

最佳答案

您可以使用 LEAD解析函数

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

  • 其他
  • LAG and LEAD Analytic Functions
  • 关于sql - 单列数据差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14766459/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com