-
-
Notifications
You must be signed in to change notification settings - Fork 102
LONDON_10 || SAIM KORKMAZ || Big-Spender SQL Week-2 #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8267b37
5214ce2
9419cf2
8d99167
8f9153f
1b3bd1e
765ca7c
bff6b81
fbc1ef6
512f710
27882d9
e48f1f9
235d7b4
39b0c86
be42962
5df5f17
1686f10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,39 +68,39 @@ 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? | ||
|
|
||
| **You:** No worries. Here's the query for that: | ||
|
|
||
| ```sql | ||
| INSERT YOUR QUERY HERE | ||
| select spends.* from spends join expense_areas on spends.expense_area_id = expense_areas.id where expense_areas.expense_area like 'Better Hospital Food'; | ||
| ``` | ||
|
|
||
| **Claire:** Great, that's very helpful. How about the total amount spent for each month? | ||
|
|
||
| **You:** You can get that by using the GROUP BY clause. Here's the query: | ||
|
|
||
| ```sql | ||
| CREATE YOUR QUERY HERE | ||
| select date_trunc('month', date), sum(amount) from spends group by date_trunc('month', date); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good use of data_trunc, but what would you do if there were different years? |
||
| ``` | ||
|
|
||
| **Farnoosh:** Thanks, that's really useful. We also need to know the total amount spent on each supplier. Can you help us with that? | ||
|
|
||
| **You:** Sure thing. Here's the query for that: | ||
|
|
||
| ```sql | ||
| INSERT YOUR QUERY HERE | ||
| select supplier_id, sum(amount) from spends group by supplier_id order by supplier_id; | ||
| ``` | ||
|
|
||
| **Farnoosh:** Oh, how do I know who these suppliers are? There's only numbers here. | ||
|
|
||
| **You:** Whoops! I gave you ids to key the totals, but let me give you names instead. | ||
|
|
||
| ```sql | ||
| INSERT YOUR QUERY HERE | ||
| select suppliers.supplier, sum(amount) from spends join suppliers on spends.supplier_id = suppliers.id group by suppliers.id order by suppliers.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 +112,7 @@ INSERT YOUR QUERY HERE | |
| **You:** Then you need an extra clause. Here's the query: | ||
|
|
||
| ```sql | ||
| CREATE YOUR QUERY HERE | ||
| select supplier_id, sum(amount) from spends where date in ('2021-03-01', '2021-04-01') group by supplier_id order by supplier_id; | ||
| ``` | ||
|
|
||
| **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 +124,7 @@ 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(date, amount, description, transaction_no, supplier_inv_no, supplier_id, expense_type_id, expense_area_id) values ('2021-08-19', 32000, 'Computer Hardware Dell', 38104091, 3780119655, 66, 42, 46); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add the other queries you used to create the records with IDs 66, 42, and 46. |
||
| ``` | ||
|
|
||
| **Claire:** Great, that's everything we need. Thanks for your help. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're looking for a specific text, you don't need to use LIKE command. It's better to just use
=here