Oracle SQL 从一列计算增长值
Oracle SQL Calculate growth value from ONE column
我要计算
INCOME_growth = INCOME_YTD - Income_prv_Month
我试过滞后功能,自我加入我无法让它工作
示例(起点)table_employee_info:
| Date |Employee_NO|INCOME_YTD |Date_ID|
--- |--- |--- |--- |
| 1/31/2022 |1002 |50 |202230
| 2/28/2022 |1045 |80 |202260
| 3/31/2022 |F104 |40 |202290
| 1/31/2022 |1002 |30 |202230
| 2/28/2022 |1045 |90 |202260
| 3/31/2022 |F104 |100 |202290
| 1/31/2022 |1002 |65 |202230
| 2/28/2022 |1045 |60 |202260
| 3/31/2022 |F104 |200 |202290
期望的结果将如下所示:
首先,我们需要按 Employee_NO 然后按日期订购。
其次,计算新列 Income_prv_Month。
三、计算另一列INCOME_growth.
| Date |Employee_NO|INCOME_YTD |Income_prv_Month|INCOME_growth|Date_ID|
--- |--- |--- |--- |--- |---
| 1/31/2022 |1002 |50 |0 |50 |202230
| 1/31/2022 |1002 |30 |50 |-20 |202230
| 1/31/2022 |1002 |65 |30 |30 |202230
| 2/28/2022 |1045 |80 |0 |80 |202260
| 2/28/2022 |1045 |90 |80 |10 |202260
| 2/28/2022 |1045 |60 |90 |-30 |202260
| 3/31/2022 |F104 |40 |0 |40 |202290
| 3/31/2022 |F104 |100 |40 |60 |202290
| 3/31/2022 |F104 |200 |100 |100 |202290
如有任何提示或帮助,我将不胜感激
提前致谢
我认为您可以尝试使用 LAG
window 函数,但真正的问题是,如果有我们可以将列替换为下面的查询 [column which represnt your expect order]
部分
SELECT t1.*,
LAG(INCOME_YTD,1,0) OVER(PARTITION BY Employee_NO ORDER BY date_ID ) Income_prv_Month,
INCOME_YTD - LAG(INCOME_YTD,1,0) OVER(PARTITION BY Employee_NO ORDER BY date_ID ) INCOME_growth
FROM T t1
我要计算
INCOME_growth = INCOME_YTD - Income_prv_Month
我试过滞后功能,自我加入我无法让它工作
示例(起点)table_employee_info:
| Date |Employee_NO|INCOME_YTD |Date_ID|
--- |--- |--- |--- |
| 1/31/2022 |1002 |50 |202230
| 2/28/2022 |1045 |80 |202260
| 3/31/2022 |F104 |40 |202290
| 1/31/2022 |1002 |30 |202230
| 2/28/2022 |1045 |90 |202260
| 3/31/2022 |F104 |100 |202290
| 1/31/2022 |1002 |65 |202230
| 2/28/2022 |1045 |60 |202260
| 3/31/2022 |F104 |200 |202290
期望的结果将如下所示: 首先,我们需要按 Employee_NO 然后按日期订购。 其次,计算新列 Income_prv_Month。 三、计算另一列INCOME_growth.
| Date |Employee_NO|INCOME_YTD |Income_prv_Month|INCOME_growth|Date_ID|
--- |--- |--- |--- |--- |---
| 1/31/2022 |1002 |50 |0 |50 |202230
| 1/31/2022 |1002 |30 |50 |-20 |202230
| 1/31/2022 |1002 |65 |30 |30 |202230
| 2/28/2022 |1045 |80 |0 |80 |202260
| 2/28/2022 |1045 |90 |80 |10 |202260
| 2/28/2022 |1045 |60 |90 |-30 |202260
| 3/31/2022 |F104 |40 |0 |40 |202290
| 3/31/2022 |F104 |100 |40 |60 |202290
| 3/31/2022 |F104 |200 |100 |100 |202290
如有任何提示或帮助,我将不胜感激
提前致谢
我认为您可以尝试使用 LAG
window 函数,但真正的问题是,如果有我们可以将列替换为下面的查询 [column which represnt your expect order]
部分
SELECT t1.*,
LAG(INCOME_YTD,1,0) OVER(PARTITION BY Employee_NO ORDER BY date_ID ) Income_prv_Month,
INCOME_YTD - LAG(INCOME_YTD,1,0) OVER(PARTITION BY Employee_NO ORDER BY date_ID ) INCOME_growth
FROM T t1