简体   繁体   English

Oracle SQL 滞后 function 带隔板

[英]Oracle SQL lag function with partition

I have a table which contains year, month, period, region_code, item_code, current_price columns in a mixed order as shown below:我有一个表,其中包含混合顺序的年、月、期、region_code、item_code、current_price 列,如下所示:

YEAR MONTH PERIOD时期 REGION_CODE REGION_CODE ITEM_CODE项目代码 CURRENT_PRICE时价
2020 2020 12 12 41 41 2500 2500 010111 010111 15 15
2020 2020 12 12 41 41 1500 1500 010111 010111 15 15
2021 2021 01 01 41 41 2500 2500 030333 030333 32.4 32.4
2021 2021 01 01 21 21 2500 2500 010111 010111 17 17
2020 2020 11 11 21 21 2500 2500 030333 030333 15 15
2020 2020 12 12 41 41 1500 1500 020222 020222 25 25
2020 2020 11 11 41 41 2500 2500 010111 010111 15 15
2021 2021 01 01 21 21 2500 2500 030333 030333 21.5 21.5
2020 2020 12 12 21 21 2500 2500 020222 020222 25.2 25.2
2020 2020 12 12 41 41 1500 1500 030333 030333 35 35
2020 2020 12 12 21 21 1500 1500 010111 010111 15.1 15.1
2020 2020 11 11 41 41 2500 2500 020222 020222 15 15
2021 2021 01 01 41 41 2500 2500 020222 020222 19.8 19.8
2020 2020 12 12 41 41 2500 2500 030333 030333 35 35
2020 2020 12 12 21 21 1500 1500 020222 020222 25.2 25.2
2020 2020 12 12 21 21 1500 1500 030333 030333 35.3 35.3
2020 2020 12 12 41 41 2500 2500 020222 020222 25 25
2020 2020 11 11 41 41 1500 1500 030333 030333 12.9 12.9
2020 2020 11 11 41 41 1500 1500 010111 010111 15 15
2020 2020 12 12 21 21 2500 2500 030333 030333 35.3 35.3
2021 2021 01 01 21 21 2500 2500 020222 020222 22.6 22.6
2020 2020 11 11 41 41 1500 1500 020222 020222 15 15
2020 2020 11 11 21 21 1500 1500 030333 030333 15 15
2020 2020 11 11 21 21 1500 1500 020222 020222 28 28
2020 2020 11 11 21 21 1500 1500 010111 010111 65 65
2021 2021 01 01 41 41 2500 2500 010111 010111 31 31
2020 2020 11 11 41 41 2500 2500 030333 030333 12.9 12.9
2021 2021 01 01 41 41 1500 1500 030333 030333 32.4 32.4
2021 2021 01 01 21 21 1500 1500 010111 010111 17 17
2021 2021 01 01 41 41 1500 1500 020222 020222 19.8 19.8
2020 2020 12 12 21 21 2500 2500 010111 010111 15.1 15.1
2021 2021 01 01 21 21 1500 1500 030333 030333 21.5 21.5
2021 2021 01 01 21 21 1500 1500 020222 020222 22.6 22.6
2021 2021 01 01 41 41 1500 1500 010111 010111 31 31
2020 2020 11 11 21 21 2500 2500 020222 020222 28 28
2020 2020 11 11 21 21 2500 2500 010111 010111 65 65

I want to get previous_price for each item using lag fuction but could not manage to do that.我想使用滞后函数获取每个项目的 previous_price,但无法做到这一点。 After that i want to calculate current and previous month's geometric mean for each year, month, region_code and item.之后,我想计算每年、每月、区域代码和项目的当前和上个月的几何平均值。 ANd there will be a TOTAL row for each month for each item.每个项目每个月都会有一个 TOTAL 行。 The result table i wanna get look like that: (I didn't calculate CUR_GEO_MEAN and PREV_GEO_MEAN values for the demo table)我想得到的结果表如下所示:(我没有计算演示表的 CUR_GEO_MEAN 和 PREV_GEO_MEAN 值)

YEAR MONTH REGION_CODE REGION_CODE ITEM_CODE项目代码 CUR_GEO_MEAN CUR_GEO_MEAN PREV_GEO_MEAN PREV_GEO_MEAN
2020 2020 11 11 1500 1500 010111 010111 ... ... ... ...
2020 2020 11 11 1500 1500 020222 020222 ... ... ... ...
2020 2020 11 11 1500 1500 030333 030333 ... ... ... ...
2020 2020 11 11 2500 2500 010111 010111 ... ... ... ...
2020 2020 11 11 2500 2500 020222 020222 ... ... ... ...
2020 2020 11 11 2500 2500 030333 030333 ... ... ... ...
2020 2020 11 11 TOTAL全部的 010111 010111 ... ... ... ...
2020 2020 11 11 TOTAL全部的 020222 020222 ... ... ... ...
2020 2020 11 11 TOTAL全部的 030333 030333 ... ... ... ...
2020 2020 12 12 1500 1500 010111 010111 ... ... ... ...
2020 2020 12 12 1500 1500 020222 020222 ... ... ... ...
2020 2020 12 12 1500 1500 030333 030333 ... ... ... ...
2020 2020 12 12 2500 2500 010111 010111 ... ... ... ...
2020 2020 12 12 2500 2500 020222 020222 ... ... ... ...
2020 2020 12 12 2500 2500 030333 030333 ... ... ... ...
2020 2020 12 12 TOTAL全部的 010111 010111 ... ... ... ...
2020 2020 12 12 TOTAL全部的 020222 020222 ... ... ... ...
2020 2020 12 12 TOTAL全部的 030333 030333 ... ... ... ...
2021 2021 01 01 1500 1500 010111 010111 ... ... ... ...
2021 2021 01 01 1500 1500 020222 020222 ... ... ... ...
2021 2021 01 01 1500 1500 030333 030333 ... ... ... ...
2021 2021 01 01 2500 2500 010111 010111 ... ... ... ...
2021 2021 01 01 2500 2500 020222 020222 ... ... ... ...
2021 2021 01 01 2500 2500 030333 030333 ... ... ... ...
2021 2021 01 01 TOTAL全部的 010111 010111 ... ... ... ...
2021 2021 01 01 TOTAL全部的 020222 020222 ... ... ... ...
2021 2021 01 01 TOTAL全部的 030333 030333 ... ... ... ...

I have tried to calculate by writing loads of queries but all failed.我试图通过编写大量查询来计算,但都失败了。 I am kinda newbie to this field so everything you show/suggest will help me much.我是这个领域的新手,所以你展示/建议的一切都会对我有很大帮助。 Thanks in advance.提前致谢。

This query should be pretty self-explained and should at least point you to the solution if there are some details that I didn't catch.这个查询应该很容易解释,如果有一些我没有抓住的细节,至少应该为您指出解决方案。

with geo as (
 select YEAR, MONTH, REGION_CODE,ITEM_CODE, exp(avg(ln(current_price))) CUR_GEO_MEAN   from tab
 group by YEAR, MONTH,REGION_CODE,ITEM_CODE 
   union all
 select YEAR, MONTH, 'TOTAL' REGION_CODE,ITEM_CODE, exp(avg(ln(current_price))) CUR_GEO_MEAN   from tab
 group by YEAR, MONTH, ITEM_CODE)
select 
 YEAR, MONTH, REGION_CODE, ITEM_CODE, CUR_GEO_MEAN,
 lag(CUR_GEO_MEAN) over (partition by REGION_CODE, ITEM_CODE order by YEAR, MONTH) as PREV_GEO_MEAN
from geo
order by YEAR, MONTH, REGION_CODE, ITEM_CODE; 

You may want to adapt the LAG function with a different partitioning and/or order or if you want to account for the missign months.您可能希望使用不同的分区和/或顺序来调整LAG function,或者如果您想考虑丢失月份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM