-
-
Notifications
You must be signed in to change notification settings - Fork 103
NW6 | Fathi Kahin | Module-DataBases | Big_Spender | week 2 #160
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
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,9 @@ 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 amount | ||
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,15 +70,20 @@ INSERT YOUR QUERY HERE | |
**You:** Then here's the query for that: | ||
|
||
```sql | ||
INSERT YOUR QUERY HERE | ||
SELECT * | ||
FROM spends | ||
WHERE description LIKE '%fee%'; | ||
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 approach and query is on the right track. However, based on the scenario question, "fee" might be inconsistently capitalised in the data. To avoid missing results, the WHERE clause should account for this. What postgresql operator allows for case insensitive matching? Compare your results |
||
``` | ||
|
||
**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 sp.date, sp.description, expense.expense_area | ||
FROM spends sp | ||
JOIN expense_areas expense ON (sp.expense_area_id = expense.id) | ||
WHERE expense.expense_area LIKE '%Better Hospital Food%'; | ||
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 joins! |
||
``` | ||
|
||
**Claire:** Great, that's very helpful. How about the total amount spent for each month? | ||
|
@@ -92,15 +99,19 @@ CREATE YOUR QUERY HERE | |
**You:** Sure thing. Here's the query for that: | ||
|
||
```sql | ||
INSERT YOUR QUERY HERE | ||
SELECT to_char(date,'YYYY-MM') AS month, sum(amount) | ||
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 job with the date formatting. well done |
||
FROM spends | ||
GROUP BY month; | ||
``` | ||
|
||
**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 sp.amount, sup.supplier | ||
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. The question is for the total amount spent by each supplier. Which operator missing here is needed to get the desired result? |
||
FROM spends sp | ||
JOIN suppliers sup ON (sp.supplier_id = sup.id); | ||
``` | ||
|
||
**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 +123,10 @@ INSERT YOUR QUERY HERE | |
**You:** Then you need an extra clause. Here's the query: | ||
|
||
```sql | ||
CREATE YOUR QUERY HERE | ||
SELECT date, SUM(amount) | ||
FROM spends | ||
WHERE date IN ('2021-03-01', '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,7 +138,8 @@ 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 (expense_type_id, expense_area_id, supplier_id, date, transaction_no, supplier_inv_no, description, amount) | ||
VALUES (7, 18, 16, '2021-08-19', 38104091, '3780119655', 'Computer Hardware Dell', 32000); | ||
|
||
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 attempt here. The rows ' Dell', 'Hardware' and 'IT' are missing from other tables, what extra step is needed to have the rows populated where necessary? What effect will this change have on the 'id's inserted in spends table? |
||
``` | ||
|
||
|
@@ -134,7 +149,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 |
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.
Almost there! Requirement is to find out the transactions between 30,000 and 31,000, not just the amount. How will your select statement be amended to get the desired result?