Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Lab 6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
USE sakila
-- Creating a View
CREATE VIEW customer_rental_summary AS
SELECT
c.customer_id,
CONCAT(c.first_name, ' ', c.last_name) AS customer_name,
c.email,
COUNT(r.rental_id) AS rental_count
FROM
customer c
JOIN
rental r ON c.customer_id = r.customer_id
GROUP BY
c.customer_id, c.first_name, c.last_name, c.email;

-- Creating a Temporary Table
CREATE TEMPORARY TABLE customer_payment_summary AS
SELECT
c.customer_id,
SUM(p.amount) AS total_paid
FROM
customer c
JOIN
payment p ON c.customer_id = p.customer_id
GROUP BY
c.customer_id;

-- Creating a CTE and the Customer Summary Report
WITH customer_summary AS (
SELECT
crs.customer_name,
crs.email,
crs.rental_count,
cps.total_paid
FROM
customer_rental_summary crs
JOIN
customer_payment_summary cps ON crs.customer_id = cps.customer_id
)
SELECT
customer_name,
email,
rental_count,
total_paid,
(total_paid / NULLIF(rental_count, 0)) AS average_payment_per_rental
FROM
customer_summary;