-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ben Brumm
committed
Jul 31, 2024
1 parent
4cebcf4
commit 7963996
Showing
4 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Dynamic Pivot | ||
*/ | ||
|
||
--SQL 01 | ||
SELECT | ||
c.country_name, | ||
SUM(CASE WHEN EXTRACT('year' FROM co.order_date) = 2021 THEN 1 ELSE 0 END) AS orders_2021, | ||
SUM(CASE WHEN EXTRACT('year' FROM co.order_date) = 2022 THEN 1 ELSE 0 END) AS orders_2022, | ||
SUM(CASE WHEN EXTRACT('year' FROM co.order_date) = 2023 THEN 1 ELSE 0 END) AS orders_2023, | ||
SUM(CASE WHEN EXTRACT('year' FROM co.order_date) = 2024 THEN 1 ELSE 0 END) AS orders_2024 | ||
FROM cust_order co | ||
INNER JOIN address a ON co.dest_address_id = a.address_id | ||
INNER JOIN country c ON a.country_id = c.country_id | ||
GROUP BY c.country_name | ||
ORDER BY c.country_name ASC; | ||
|
||
--SQL 02 | ||
SELECT * | ||
FROM crosstab( | ||
'SELECT | ||
c.country_name, | ||
EXTRACT(''year'' FROM co.order_date) AS order_year, | ||
COUNT(*) AS rowcount | ||
FROM cust_order co | ||
INNER JOIN address a ON co.dest_address_id = a.address_id | ||
INNER JOIN country c ON a.country_id = c.country_id | ||
GROUP BY c.country_name, order_year' | ||
) | ||
AS ct(country_name text, "2021" numeric, "2022" numeric); | ||
|
||
--SQL 03 | ||
CREATE extension tablefunc; |