0

I am trying to display the supplier code, supplier name and postcode of all suppliers who satisfy every stock item they supply having a price value above the average of all stock items.

I have been trying for a while, and this is the best I can come up with, although it isn't working

SELECT SuppName, SuppCode, Postcode
FROM Suppliers 
WHERE Suppliers.SuppCode = (
SELECT SuppCode
FROM Stocks 
WHERE Suppliers.SuppCode = Stocks.SuppCode
GROUP BY SuppCode
HAVING MIN(Price) > AVG(Price)
);

If anyone could give me a hint in the right direction as to where I am going wrong it would really be appreciated.

   create table Stocks
  (StockNo      number(6) not null primary key,
   StoreCode    varchar2(6) not null, 
   Description  varchar2(24),
   Quantity     number(6),
   Units        varchar2(12),
   Reorder      number(6),
   foreign key (StoreCode)
     references Stores(StoreCode));

   create table Suppliers
  (SuppCode  varchar2(4) not null primary key,
   SuppName  varchar2(30),
   Street    varchar2(24),
   Town      varchar2(16),
   County    varchar2(16),
   PostCode  varchar2(10),
   TelNo     varchar2(16),
   FaxNo     varchar2(16))
  cluster SupplyData(SuppCode);

1 Answer 1

1

To get the codes for these supplies, I would go for aggregation. You seem to want:

SELECT s.SuppCode
FROM Stocks s
GROUP BY s.SuppCode
HAVING MIN(s.Price) > (SELECT AVG(s2.Price) FROM Stocks s2);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer, I had this at one point but was then struggling with obtaining the supplier name and postcode aswell because I can't group when using two tables. I Tried using it as a sub query but it returns multiple rows so I was a bit stumped and ended up trying other solutions. Sorry i'm still on a learning curve with oracle, am I missing something obvious?
@cmorga1 . . . You can get the supplier name by using an explicit JOIN or IN operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.