0% found this document useful (0 votes)
2 views

Day+11+-+Course+slides

The document contains SQL queries demonstrating the use of window functions for data aggregation, including calculating sums and counts partitioned by customer and payment type. It also provides examples of queries to retrieve movie details, payment information, flight delays, customer payments, daily revenue, and staff revenue shares. Each query is designed to extract specific insights from the sales and customer data while utilizing various SQL functionalities.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Day+11+-+Course+slides

The document contains SQL queries demonstrating the use of window functions for data aggregation, including calculating sums and counts partitioned by customer and payment type. It also provides examples of queries to retrieve movie details, payment information, flight delays, customer payments, daily revenue, and staff revenue shares. Each query is designed to extract specific insights from the sales and customer data while utilizing various SQL functionalities.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Day 11

Window functions
SELECT
transaction_id,
payment_type, No. of rows not affected
customer_id,
price_in transaction,
(SELECT SUM(price_in_transaction) Aggregated by
FROM sales s2
WHERE s2.customer_id=s1.customer_id)
FROM sales s1
Window functions
SELECT
transaction_id,
payment_type, No. of rows not affected
customer_id,
price_in transaction,
SUM(price_in_transaction) OVER(PARTITION BY customer_id) Aggregated by
FROM sales s
Window functions
SELECT
transaction_id,
payment_type, No. of rows not affected
customer_id,
price_in transaction,
COUNT(*) OVER(PARTITION BY customer_id) Aggregated by
FROM sales s
Window functions
SELECT
transaction_id,
payment_type, No. of rows not affected
customer_id,
price_in transaction,
COUNT(*) OVER(PARTITION BY payment_type) Aggregated by
FROM sales s
Window functions

No. of rows not affected Aggregated by


OVER()

No. of rows not affected Aggregated by

SUM() COUNT() RANK() FIRST_VALUE() LEAD() LAG()


Window functions

AGG(agg_column) OVER(PARTITION BY partition_column)

Window
Write a query that returns the list of movies including
- film_id,
- title,
- length,
- category,
- average length of movies in that category.

Order the results by film_id.

Result
Write a query that returns all payment details including
- the number of payments that were made by this customer
and that amount

Order the results by payment_id.

Result
Write a query that returns the running total of how late the flights are
(difference between actual_arrival and scheduled arrival) ordered by flight_id
including the departure airport.

As a second query, calculate the same running total but partition also by the
departure airport.

Result
Write a query that returns the customers' name, the country and how many
payments they have. For that use the existing view customer_list.

Afterwards create a ranking of the top customers with most sales for each
country. Filter the results to only the top 3 customers per country.

Result
Write a query that returns the revenue of the day and the revenue of the
previous day.

Afterwards calculate also the percentage growth compared to the previous


day.

Result
Write a query that calculates now the share of revenue each staff_id makes per
customer. The result should look like this:

Hint
You need to use a subquery.

You might also like