Forked from dgadiraju/spark-sql-windowing-functions.py
Created
September 15, 2019 15:07
-
-
Save rajjnish/9619b98b60f68d03944f0611082a349f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| select * from ( | |
| select o.order_id, o.order_date, o.order_status, oi.order_item_subtotal, | |
| round(sum(oi.order_item_subtotal) over (partition by o.order_id), 2) order_revenue, | |
| oi.order_item_subtotal/round(sum(oi.order_item_subtotal) over (partition by o.order_id), 2) pct_revenue, | |
| round(avg(oi.order_item_subtotal) over (partition by o.order_id), 2) avg_revenue, | |
| rank() over (partition by o.order_id order by oi.order_item_subtotal desc) rnk_revenue, | |
| dense_rank() over (partition by o.order_id order by oi.order_item_subtotal desc) dense_rnk_revenue, | |
| percent_rank() over (partition by o.order_id order by oi.order_item_subtotal desc) pct_rnk_revenue, | |
| row_number() over (partition by o.order_id order by oi.order_item_subtotal desc) rn_orderby_revenue, | |
| row_number() over (partition by o.order_id) rn_revenue, | |
| lead(oi.order_item_subtotal) over (partition by o.order_id order by oi.order_item_subtotal desc) lead_order_item_subtotal, | |
| lag(oi.order_item_subtotal) over (partition by o.order_id order by oi.order_item_subtotal desc) lag_order_item_subtotal, | |
| first_value(oi.order_item_subtotal) over (partition by o.order_id order by oi.order_item_subtotal desc) first_order_item_subtotal, | |
| last_value(oi.order_item_subtotal) over (partition by o.order_id order by oi.order_item_subtotal desc) last_order_item_subtotal | |
| from orders o join order_items oi | |
| on o.order_id = oi.order_item_order_id | |
| where o.order_status in ('COMPLETE', 'CLOSED')) q | |
| where order_revenue >= 1000 | |
| order by order_date, order_revenue desc, rnk_revenue; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment