From b1522806e46dc33d50219e5879a9f7592a0c6ba3 Mon Sep 17 00:00:00 2001 From: Sorrel Harriet <s.harriet@gold.ac.uk> Date: Sun, 16 Oct 2016 22:14:02 +0100 Subject: [PATCH] minor update to queries.sql --- lab-3/music-store-app/sql/queries.sql | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lab-3/music-store-app/sql/queries.sql b/lab-3/music-store-app/sql/queries.sql index 5df13d3..50ec236 100644 --- a/lab-3/music-store-app/sql/queries.sql +++ b/lab-3/music-store-app/sql/queries.sql @@ -70,7 +70,7 @@ DELETE FROM Track WHERE album_upc='726517237627'; /* EXAMPLE */ /* ------------------------------------ */ -/* 5. Retrieve details about an order +/* 5a. Retrieve details about an order Include the customer_id, delivery method and total order value in the result-set */ @@ -81,3 +81,20 @@ INNER JOIN LineItem INNER JOIN Album ON Album.upc=LineItem.album_upc WHERE Transaction.id=1; /* EXAMPLE */ + +/* 5b. Retrieve details about an order +Include the transaction id, customer_id +and the number of items ordered in the +result-set */ +SELECT t.id, t.customer_id, SUM(li.quantity) AS num_items +FROM Transaction t +INNER JOIN LineItem li + ON t.id=li.trans_id +GROUP BY t.id; /* group results by transaction id */ + +/* 5c. Does the same as the previous query +but using a nested query rather than a JOIN */ +SELECT t.id, t.customer_id, +(SELECT SUM(li.quantity) FROM LineItem li + WHERE li.trans_id=t.id) AS num_items +FROM Transaction t; -- GitLab