Skip to content
Open
Changes from 1 commit
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
43 changes: 33 additions & 10 deletions Big-Spender/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ 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 transaction_no Between 30000 And 31000;

Choose a reason for hiding this comment

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

Not quite correct here - at the moment when i run this query I get a empty table, but I should be seeing all transactions that spent ammounts between £30,000 and £31,000 - have a look again at this query.

```

**Claire:** That's great, thanks. Hey, what about transactions that include the word 'fee' in their description?
Expand All @@ -68,39 +69,52 @@ 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 transaction_no, expense_area
from spends
Join expense_areas
On spends.expense_area_id = expense_areas.id
Where expense_area ILIKE 'Better Hospital Food';
```

Choose a reason for hiding this comment

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

Unsure about your choice to not include any other information except transaction_no and expense_area, as an output it's very uninformative. Especially as the key information will be amounts spent, suppliers, dates etc.

Copy link
Author

Choose a reason for hiding this comment

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

My initial understating was that the requirements were transactions number and the expense area of 'Better Hospital Food'. Now the query shows all the other information needed from the tables.


**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', spends.date) AS month

Choose a reason for hiding this comment

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

You could approach this query with the mindset of "I am only going to give information that is informative to this request".

In this case, simply knowing the month alone is what the finance person is requesting.

So how could you turn the output of a timestamp rounded to a month into a column of just data identifying which month it is. No specific dates, no timestamps, just a month.

, SUM(spends.amount) AS total_spend
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?

**You:** Sure thing. Here's the query for that:

```sql
INSERT YOUR QUERY HERE
Select supplier_id, SUM(amount) AS total_spent
From spends
Group by supplier_id
Order by total_spent 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 description, SUM(amount) AS total_spent

Choose a reason for hiding this comment

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

We aren't looking for "description" here. In the database you can see a suppliers table which is joined to the spends table via the supplier_id.

How can you use this relationship to pull in the supplier name instead of the "description" of the charge?

Copy link
Author

Choose a reason for hiding this comment

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

Description column replaced by amount column so the information need are accessible now.

From spends
Group by description
Order by total_spent 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 +126,12 @@ INSERT YOUR QUERY HERE
**You:** Then you need an extra clause. Here's the query:

```sql
CREATE YOUR QUERY HERE
Select date_trunc('day', spends.date) As day,
Sum(amount) As total_spent
From spends
Where date IN ('2021-03-01', '2021-04-01')
Group by day
Order by day;
```

**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 +143,12 @@ 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
(amount, date, transaction_no, supplier_inv_no, description, expense_type_id, expense_area_id)
Values
(32000, '2021-08-19', 38104091, 3780119655, 'Dell',
(SELECT id FROM expense_types WHERE expense_type = 'Hardware'),
(SELECT id FROM expense_areas WHERE expense_area = 'IT'));
```

**Claire:** Great, that's everything we need. Thanks for your help.
Expand Down