Created
December 21, 2021 15:43
-
-
Save 1ambda/18ecbc382074b9baa48b01da33812871 to your computer and use it in GitHub Desktop.
This file contains 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
spark.sql(""" | |
WITH CALCULATED ( | |
SELECT | |
brand, | |
sum(price) as price_sum | |
FROM PURCHASE | |
WHERE | |
brand IS NOT NULL | |
GROUP BY brand | |
), | |
RANKED ( | |
SELECT | |
brand, | |
lag(price_sum, 1) OVER (PARTITION BY 1 ORDER BY price_sum DESC) as price_sum_prev, | |
price_sum as price_sum_current, | |
lead(price_sum, 1) OVER (PARTITION BY 1 ORDER BY price_sum DESC) as price_sum_next, | |
dense_rank() OVER (PARTITION BY 1 ORDER BY price_sum DESC) as rank | |
FROM CALCULATED | |
) | |
SELECT * | |
FROM RANKED | |
ORDER BY rank ASC | |
""").show(truncate=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment