Skip to content

NW6 | Sabella Fisseha | Module-DataBases | Big_Spender | week 2 #159

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

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
55 changes: 52 additions & 3 deletions Big-Spender/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ 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 and DATE_PART('year',date)= 2021;

```

big-spender=# select \* from spends where amount between 30000 and 31000 and DATE_PART('year',date)= 2021;

**Claire:** That's great, thanks. Hey, what about transactions that include the word 'fee' in their description?

**You:** Does case matter?
Expand All @@ -67,24 +72,38 @@ INSERT YOUR QUERY HERE

**You:** Then here's the query for that:

```sql
<!-- ```select * from spends where description ilike '%FeE%' -->

INSERT YOUR QUERY HERE
```

````
select * from spends where description ilike '%FeE%'

Choose a reason for hiding this comment

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

Good work taking capitalisations into consideration with 'ilike'. note you don't have to mix cases in the word, e.g '%FeE%' can simply be written as '%fee%'. ILIKE already handles the case insensitive matching


**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.*, expense_areas.expense_area
FROM spends
INNER JOIN expense_areas ON expense_areas.id = spends.expense_area_id

Choose a reason for hiding this comment

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

Good use of joins!

WHERE expense_areas.expense_area = '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)::date AS month, SUM(amount) AS total

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

FROM spends
GROUP BY month
ORDER 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?
Expand All @@ -93,6 +112,14 @@ CREATE YOUR QUERY HERE

```sql
INSERT YOUR QUERY HERE


SELECT SUM(spends.amount) AS total_amount
FROM spends
INNER JOIN suppliers ON suppliers.id = spends.supplier_id

Choose a reason for hiding this comment

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

Great job with the joins! However, two columns are expected in the results. What additional information from the suppliers table should be included in the SELECT statement?

GROUP BY suppliers.id
ORDER BY total_amount DESC;

```

**Farnoosh:** Oh, how do I know who these suppliers are? There's only numbers here.
Expand All @@ -101,6 +128,12 @@ INSERT YOUR QUERY HERE

```sql
INSERT YOUR QUERY HERE
SELECT suppliers.*, SUM(spends.amount) AS total_amount
FROM spends
INNER JOIN suppliers ON suppliers.id = spends.supplier_id
GROUP BY suppliers.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 @@ -113,6 +146,12 @@ INSERT YOUR QUERY HERE

```sql
CREATE YOUR QUERY HERE

SELECT date, SUM(amount) AS total_amount
FROM spends
WHERE date BETWEEN '2021-03-01' AND '2021-04-01'

Choose a reason for hiding this comment

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

Good attempt. Note that total amounts for only those two dates are required, and not every date inbetween. Which operator will allow for multiple distinct values to be specified in your WHERE statement?

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 @@ -132,6 +171,16 @@ INSERT YOUR QUERIES HERE

**You:** No problem, glad I could help you out.

select id from suppliers where supplier = 'Dell';
insert into suppliers(supplier) values ('Dell');
select id from expense_types where expense_type = 'Hardware';
insert into expense_types (expense_type) values ('Hardware');
select id from expense_areas where expense_area = 'IT';
insert into expense_areas (expense_area) values ('IT');

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.

INSERT INTO spends (expense_type_id,expense_area_id,supplier_id, date,transaction_no,supplier_inv_no, description, amount)
VALUES (42, 46,66,'2021-08-19',38104091,3780119655, 'Computer Hardware Dell', 32000);

## Acceptance Criteria

- [ ] All user stories are satisfied
Expand Down