diff --git a/lab-3/music-store-app/sql/queries.sql b/lab-3/music-store-app/sql/queries.sql index 5df13d305b8051750413c5d0eba3ff67003b4f56..50ec236db6f49ebd519353cab8562177816d47bf 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;