Created
December 21, 2021 15:39
-
-
Save 1ambda/f667505f10a2b4f05b8b99ee4592b246 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 | |
CAST(event_time AS DATE) as event_date, | |
brand, | |
sum(price) as price_sum | |
FROM PURCHASE | |
WHERE | |
brand IS NOT NULL | |
GROUP BY 1, 2 | |
), | |
RANKED ( | |
SELECT | |
event_date, | |
brand, | |
price_sum, | |
rank() OVER (PARTITION BY event_date ORDER BY price_sum DESC) as rank | |
FROM CALCULATED | |
) | |
SELECT * | |
FROM RANKED | |
WHERE rank <= 3 | |
ORDER BY event_date ASC, rank ASC | |
""").show(truncate=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment