SQL - 显示最畅销的欧洲汽车模型

时间:2015-05-28 12:57:39

标签: sql oracle

我有这个问题:

显示最畅销的欧洲汽车的详细信息(即制造商名称,型号名称,类型) 模型。

表格是:

制造商

CREATE TABLE Manufacturer(
manufacturerID    VARCHAR2(10)  NOT NULL,
name     VARCHAR2(20),
region   VARCHAR2(20) CHECK(region IN('ASIA', 'AFRICA', 'NORTH AMERICA', 'SOUTH AMERICA', 'EUROPE', 'OCEANIA')),
PRIMARY KEY(manufacturerID));

SALESTRANSACTION

CREATE TABLE SalesTransaction(
VIN    VARCHAR2(17)  NOT NULL,
custID   VARCHAR2(10) NOT NULL,
agentID   VARCHAR2(10) NOT NULL,
dateOfSale DATE,
agreedPrice NUMBER(10,2),
PRIMARY KEY(VIN, custID, agentID),
FOREIGN KEY(VIN) REFERENCES Car(VIN),
FOREIGN KEY(custID) REFERENCES Customer(custID),
FOREIGN KEY(agentID) REFERENCES SalesAgent(agentID));

CAR

CREATE TABLE Car(
VIN      VARCHAR2(17)  NOT NULL,
dateAcquired   DATE,
yearBuilt  NUMBER(4) CHECK(yearBuilt >1980),
purchasedPrice  NUMBER(10,2),
askingPrice NUMBER(10,2),
currentMileage NUMBER(7),
modelNo VARCHAR2(10),
PRIMARY KEY(VIN),
FOREIGN KEY(modelNo) REFERENCES Model(modelNo));

MODEL

CREATE TABLE Model(  
    modelNo      VARCHAR2(10)  NOT NULL,
    name   VARCHAR2(20),
    type VARCHAR2(20) CHECK(type IN('CONVERTIBLE', 'HYBRID', 'LUXURY', 'SUV', 'VAN', 'COUPE', 'SEDAN', 'WAGON', 'HATCH', 'SPORTS', 'ROADSTER')),
    previousModel VARCHAR2(10),
    manufacturerID VARCHAR2(10),
    PRIMARY KEY(modelNo),
    FOREIGN KEY(previousModel) REFERENCES Model(modelNo),
    FOREIGN KEY(manufacturerID) REFERENCES Manufacturer(manufacturerID));

到目前为止,我做到了这一点:

SELECT Manufacturer.name, Model.name, Model.type
FROM Manufacturer, Model, Car, SalesTransaction
WHERE Manufacturer.region = 'EUROPE' 
AND Model.modelNo = Car.modelNo 
AND SalesTransaction.VIN = Car.VIN
AND Model.manufacturerID = Manufacturer.manufacturerID;

我得到了所有的车,但我只想要最畅销的欧洲车。 我应该使用ORDER BY和rownum = 1吗?如果这是一种可能的方法,我该怎么做?我很困惑怎么做。

1 个答案:

答案 0 :(得分:0)

首先应通过group by获取总销售额,然后使用order by订购结果。

SELECT Manufacturer.name, Model.name, Model.type, sum(st.agreedPrice) total_sales
FROM Manufacturer, Model, Car, SalesTransaction st
WHERE Manufacturer.region = 'EUROPE' 
AND Model.modelNo = Car.modelNo 
AND SalesTransaction.VIN = Car.VIN
AND Model.manufacturerID = Manufacturer.manufacturerID
group by Manufacturer.name, Model.name, Model.type
order by sum(st.agreedPrice) desc; -- order desc is important

然后,您可以在此基础上使用rownum选择第一行。

select * from (
    SELECT Manufacturer.name manufacturer_name, 
           Model.name model_name, 
           Model.type, 
           sum(st.agreedPrice) total_sales
    FROM Manufacturer, Model, Car, SalesTransaction st
    WHERE Manufacturer.region = 'EUROPE' 
    AND Model.modelNo = Car.modelNo 
    AND SalesTransaction.VIN = Car.VIN
    AND Model.manufacturerID = Manufacturer.manufacturerID
    group by Manufacturer.name, Model.name, Model.type
    order by sum(st.agreedPrice) desc -- order desc is important
) where rownum <= 1;

这是一种实现它的方法。还有一些相关问题,例如“当汽车与第一个地点并列时会发生什么?”你也需要考虑。

根据您的场景,您可以考虑使用oracle分析函数,例如rank()或row_number()。

Oracle RANK

Oracle DENSE_RANK

ORACLE ROW_NUMBER