Skip to content
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
38 changes: 35 additions & 3 deletions Big-Spender/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ You are working with Claire and Farnoosh, who are trying to complete a missing r

```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 @@ -69,6 +70,8 @@ INSERT YOUR QUERY HERE

```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?
Expand All @@ -77,6 +80,9 @@ INSERT YOUR QUERY HERE

```sql
INSERT YOUR QUERY HERE
SELECT spends.* FROM spends
INNER JOIN expense_areas
ON (expense_areas.id = spends.expense_area_id) AND expense_areas.expense_area = 'Better Hospital Food';
```

**Claire:** Great, that's very helpful. How about the total amount spent for each month?
Expand All @@ -85,14 +91,26 @@ INSERT YOUR QUERY HERE

```sql
CREATE YOUR QUERY HERE
SELECT
DATE_TRUNC('month', date)::DATE AS month,
SUM(amount) AS total
FROM spends
GROUP BY DATE_TRUNC('month', date)::DATE;
```

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

**Farnoosh:** Oh, how do I know who these suppliers are? There's only numbers here.
Expand All @@ -112,7 +130,9 @@ INSERT YOUR QUERY HERE
**You:** Then you need an extra clause. Here's the query:

```sql
CREATE YOUR QUERY HERE
SELECT SUM(amount)
FROM spends
WHERE date BETWEEN '2021-03-01' AND '2021-04-01';
```

**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,7 +144,19 @@ 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 suppliers (supplier) VALUES ('Dell');
INSERT INTO expense_types (expense_type) VALUES ('Hardware');
INSERT INTO expense_areas (expense_area) VALUES ('IT');
INSERT INTO spends (expense_type_id, expense_area_id, supplier_id, date, transaction_no, supplier_inv_no, description, amount)
VALUES (124,
136,
196,
'2021-08-19',
38104091,
'3780119655',
'Computer Hardware Dell',
32000);


```

Expand Down