0

Calculate each supplier's total sales quantity and get the sales person's name if the sales person supplies parts more than 1000 units in total.

Table info:

Supplier {s_num, s_name, status, city}

Spj {s_num, p_num, j_num, qty}

This is what I have:

SELECT s_name, SUM(qty) AS sum
FROM Supplier, Spj
WHERE Supplier.s_num = Spj.s_num
AND qty > 1000
GROUP BY s_name;

I think my error is in this line:

AND qty > 1000

maybe I am using the "GROUP BY" incorrectly...

No suppliers quantity is above 800, so I get a blank result.

I want to test this:

"sum of quantity for each supplier" > 1000
Steven
  • 7
  • 4

1 Answers1

0

Use HAVING to access aggregate results after a group by clause:

SELECT s_name, SUM(qty) AS sum
FROM Supplier, Spj
WHERE Supplier.s_num = Spj.s_num
GROUP BY s_name
HAVING qty > 1000;

See this post for more info on having vs where.

Blue
  • 22,608
  • 7
  • 62
  • 92