我有两张桌子,
SUPPLIERS(
SUPPCODE,
SUPPNAME,
STREET,
TOWN,
COUNTRY,
POSTCODE,
TELNO,
FAXNO)
STOCKS(
STOCKNO,
STORECODE,
DESCRIPTION,
QUANTITY,
UNITS,
REORDER,
PRICE,
SUPPCODE)
我想找到供应商的名称,他们提供的所有商品的价格都高于表格中所有商品的平均价格。
我知道我可以使用
找到所有高于平均水平的物品SELECT S1.SuppCode, S1.SuppName, S1.PostCode
FROM Suppliers S1, Stocks S2
WHERE Price > (
SELECT AVG(Price)
FROM Stocks
)
AND S1.SuppCode = S2.SuppCode;
但我不确定如何确保供应商的每件商品都高于平均价格。
平均值是
AVG(PRICE)
----------
5.14411765
表格看起来像这样
SQL> select suppcode, price
2 from stocks
3 order by suppcode
4 ;
SUPPCO PRICE
------ ----------
S1 2.85
S1 2.85
S1 2.14
S1 .49
S1 .98
S1 1.59
S1 1.84
S1 1.35
S2 5.15
S2 2.98
S2 11.38
SUPPCO PRICE
------ ----------
S2 12.85
S3 2.57
S3 4.95
S3 1.89
S3 4.11
S3 8.58
S3 1.97
S3 .81
S3 6.19
S3 3.65
S4 4.83
SUPPCO PRICE
------ ----------
S4 2.42
S4 .22
S4 .45
S4 .47
S4 .42
S4 2.15
S4 3.48
S5 7.99
S5 7.85
S5 6.47
S6 23.99
SUPPCO PRICE
------ ----------
S6 32.99
所以考虑到这一点,我想得到S5和S6。
答案 0 :(得分:1)
Oracle 11g R2架构设置:
CREATE TABLE SUPPLIERS(
SUPPCODE NUMBER(5,0) PRIMARY KEY,
SUPPNAME VARCHAR2(20),
STREET VARCHAR2(20),
TOWN VARCHAR2(20),
COUNTRY VARCHAR2(20),
POSTCODE VARCHAR2(20),
TELNO NUMBER(11,0),
FAXNO NUMBER(11,0)
);
CREATE TABLE STOCKS(
STOCKNO NUMBER(5,0) PRIMARY KEY,
STORECODE VARCHAR2(20),
DESCRIPTION VARCHAR2(20),
QUANTITY NUMBER(5,0),
UNITS NUMBER(5,0),
REORDER NUMBER(5,0),
PRICE NUMBER(12,2),
SUPPCODE NUMBER(5,0),
FOREIGN KEY ( SUPPCODE ) REFERENCES SUPPLIERS ( SUPPCODE )
);
查询1 :
SELECT SUPPCODE
FROM SUPPLIERS s
WHERE NOT EXISTS (SELECT 1
FROM STOCKS k
WHERE s.SUPPCODE = k.SUPPCODE
AND price <= (SELECT AVG( price ) AS avg_price FROM STOCKS)
)
<强> Results 强>: