From 7350cb2fcedd01676a0721950edace6b54ee591e Mon Sep 17 00:00:00 2001 From: Pedro Ricciardi Date: Thu, 16 May 2024 11:04:09 +0100 Subject: [PATCH 1/2] all tasks completed --- Big-Spender/readme.md | 90 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 13 deletions(-) diff --git a/Big-Spender/readme.md b/Big-Spender/readme.md index dc6cf9a2..cd02daad 100644 --- a/Big-Spender/readme.md +++ b/Big-Spender/readme.md @@ -48,7 +48,7 @@ You are working with Claire and Farnoosh, who are trying to complete a missing r **You:** Absolutely. Here's the SQL query you need: ```sql -INSERT YOUR QUERY HERE +select * from spends where amount between 30000 and 31000; ``` **Claire:** That's great, thanks. Hey, what about transactions that include the word 'fee' in their description? @@ -68,7 +68,7 @@ INSERT YOUR QUERY HERE **You:** Then here's the query for that: ```sql -INSERT YOUR QUERY HERE +select * from spends where description ilike '%fee%'; ``` **Farnoosh:** Hi, it's me again. It turns out we also need the transactions that have the expense area of 'Better Hospital Food'. Can you help us with that one? @@ -76,7 +76,16 @@ INSERT YOUR QUERY HERE **You:** No worries. Here's the query for that: ```sql -INSERT YOUR QUERY HERE +select * from spends where expense_area_id = 2; +``` + +or + +```sql +select s.* +from spends s.* +join expense_areas ea on s.expense_area_id = ea.id +where ea.expense_area = 'Better Hospital Food'; ``` **Claire:** Great, that's very helpful. How about the total amount spent for each month? @@ -84,7 +93,11 @@ INSERT YOUR QUERY HERE **You:** You can get that by using the GROUP BY clause. Here's the query: ```sql -CREATE YOUR QUERY HERE +select + to_char(date_trunc('month', date), 'Month') as month, + sum(amount) as total_amount +from spends +group by month; ``` **Farnoosh:** Thanks, that's really useful. We also need to know the total amount spent on each supplier. Can you help us with that? @@ -92,7 +105,12 @@ CREATE YOUR QUERY HERE **You:** Sure thing. Here's the query for that: ```sql -INSERT YOUR QUERY HERE +select + supplier_id as supplier, + sum(amount) as total_amount +from spends +group by supplier +order by supplier; ``` **Farnoosh:** Oh, how do I know who these suppliers are? There's only numbers here. @@ -100,7 +118,26 @@ INSERT YOUR QUERY HERE **You:** Whoops! I gave you ids to key the totals, but let me give you names instead. ```sql -INSERT YOUR QUERY HERE +SELECT + sp.supplier_id, + su.supplier, + SUM(sp.amount) as total_amount +FROM spends sp +JOIN suppliers su ON sp.supplier_id = su.id +GROUP BY sp.supplier_id, su.supplier +ORDER BY su.supplier; +``` + +or, if you don't want the supplier id + +```sql +SELECT + su.supplier, + SUM(sp.amount) as total_amount +FROM spends sp +JOIN suppliers su ON sp.supplier_id = su.id +GROUP BY su.supplier +ORDER BY su.supplier; ``` **Claire:** Thanks, that's really helpful. I can't quite figure out...what is the total amount spent on each of these two dates (1st March 2021 and 1st April 2021)? @@ -112,7 +149,14 @@ INSERT YOUR QUERY HERE **You:** Then you need an extra clause. Here's the query: ```sql -CREATE YOUR QUERY HERE +select + to_char(date, 'DD/MM/YYYY') as date, + sum(amount) as total_amount +from spends +where +date = '2021-03-01' or +date = '2021-04-01' +group by date; ``` **Farnoosh:** Fantastic. One last thing, looks like we missed something. Can we add a new transaction to the spends table with a description of 'Computer Hardware Dell' and an amount of £32,000? @@ -124,8 +168,28 @@ CREATE YOUR QUERY HERE **You:** Sure thing. To confirm, the date is August 19, 2021, the transaction number is 38104091, the supplier invoice number is 3780119655, the supplier is 'Dell', the expense type is 'Hardware' and the expense area is 'IT'. Here's the query for that: ```sql -INSERT YOUR QUERIES HERE - +insert into spends + (description, + amount, + date, + transaction_no, + supplier_inv_no, + supplier_id, + expense_type_id, + expense_area_id) +values + ('Computer Hardware Dell', + 32000, + '2021-08-19', + 38104091, + 3780119655, + (select id from suppliers + where supplier ilike 'Dell'), + (select id from expense_types + where expense_type ilike 'Hardware'), + (select id from expense_areas + where expense_area ilike 'IT') + ); ``` **Claire:** Great, that's everything we need. Thanks for your help. @@ -134,7 +198,7 @@ INSERT YOUR QUERIES HERE ## Acceptance Criteria -- [ ] All user stories are satisfied -- [ ] All queries are written in SQL -- [ ] All queries are correct and I have tested them in the database -- [ ] I have opened a pull request with my answers written directly into this README.md file +- [x] All user stories are satisfied +- [x] All queries are written in SQL +- [x] All queries are correct and I have tested them in the database +- [x] I have opened a pull request with my answers written directly into this README.md file From 496702f62708e2f426ae781e47420e194aa3ab30 Mon Sep 17 00:00:00 2001 From: Pedro Ricciardi Date: Thu, 16 May 2024 11:16:01 +0100 Subject: [PATCH 2/2] type error solved --- Big-Spender/readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Big-Spender/readme.md b/Big-Spender/readme.md index cd02daad..59c341cd 100644 --- a/Big-Spender/readme.md +++ b/Big-Spender/readme.md @@ -83,9 +83,9 @@ or ```sql select s.* -from spends s.* +from spends s join expense_areas ea on s.expense_area_id = ea.id -where ea.expense_area = 'Better Hospital Food'; +where ea.expense_area ilike 'Better Hospital Food'; ``` **Claire:** Great, that's very helpful. How about the total amount spent for each month?