I have a bit of a dilemma. I want to do something similar to the below query, but a bit more advanced basically.
My table is structured like match_result, player_name, loser_name, tournament_name, round, etc.
I want to generate separate columns for the results of each tournament. e.g.
tournament_one_result column would have ( player_name || " " || “beat” || loser_Name || " " || etc… and then the round beat in a separate column.
But I need some way to do this for all tournaments. My first approach was to create each tournament in a separate subquery, e.g. WITH TOURNEY_ONE AS ( ___ ), but there’s over 50 tournaments so not practical.
Could anyone help me with this?
My Code:
WITH
country AS (SELECT customer_id, country, first_name, last_name FROM customer),
total_purchases AS (SELECT customer_id, SUM(total) AS total FROM invoice
GROUP BY customer_id)
SELECT country.country AS country, (country.first_name || " " || country.last_name) AS customer_name, MAX(total_purchases.total) AS total_purchased
FROM country
INNER JOIN total_purchases ON total_purchases.customer_id = country.customer_id
GROUP BY 1
ORDER BY 1;