Skip to content
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

NW6 | Fikret Ellek | Module-DataBases | [TECH ED] Big Spender | week 2 #161

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions Big-Spender/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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 LOWER(description) LIKE '%fee%';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work taking the capitlisations into consideration with 'LOWER'

```

**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 s.*, e.expense_area FROM spends s JOIN expense_areas e ON s.expense_area_id=e.id WHERE LOWER(e.expense_area) LIKE '%better hospital food%';

Choose a reason for hiding this comment

The 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?

**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)::DATE AS month, SUM(amount) AS total_amount FROM spends GROUP BY month ORDER BY month;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work with the date format in your select statement!

```

**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 su.id, su.supplier, SUM(sp.amount) AS total_amount from suppliers su JOIN spends sp ON su.id=sp.supplier_id GROUP BY su.id ORDER BY total_amount DESC;
```

**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 su.id, su.supplier, SUM(sp.amount) AS total_amount from suppliers su JOIN spends sp ON su.id=sp.supplier_id GROUP BY su.id ORDER BY total_amount DESC;
```

**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)?
Expand All @@ -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 date, SUM(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?
Expand All @@ -124,8 +124,39 @@ 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

-- Ensure the expense type exists
INSERT INTO expense_types (expense_type)
SELECT 'Hardware'
WHERE NOT EXISTS (
SELECT 1 FROM expense_types WHERE expense_type = 'Hardware'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great attention to detail! The 'Hardware', 'Dell', and 'IT' rows were missing from the other tables, and you've done well by adding the necessary insert statements to populate them.

);

-- Ensure the expense area exists
INSERT INTO expense_areas (expense_area)
SELECT 'IT'
WHERE NOT EXISTS (
SELECT 1 FROM expense_areas WHERE expense_area = 'IT'
);

-- Ensure the supplier exists
INSERT INTO suppliers (supplier)
SELECT 'Dell'
WHERE NOT EXISTS (
SELECT 1 FROM suppliers WHERE supplier = 'Dell'
);

-- Insert the new transaction into the spends table
INSERT INTO spends (expense_type_id, expense_area_id, supplier_id, date, transaction_no, supplier_inv_no, description, amount)
VALUES (
(SELECT id FROM expense_types WHERE expense_type = 'Hardware'),
(SELECT id FROM expense_areas WHERE expense_area = 'IT'),
(SELECT id FROM suppliers WHERE supplier = 'Dell'),
'2021-08-19'::date,
38104091,
'3780119655',
'Computer Hardware Dell',
32000
);
```

**Claire:** Great, that's everything we need. Thanks for your help.
Expand All @@ -134,7 +165,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