0

使用下表:

CREATE TABLE CUSTOMERS
(customerID     INT     PRIMARY KEY,
customerFullName        VARCHAR(20) NOT NULL,
customerStreet      VARCHAR(40) NOT NULL,
customerState       VARCHAR(15) NOT NULL,
customerCity        VARCHAR(20) NOT NULL,
customerZip     VARCHAR(15) NOT NULL);

CREATE TABLE VEHICLES
(vehicleVIN     VARCHAR(25) PRIMARY KEY,
vehicleType     VARCHAR(10) NOT NULL CHECK (lower(vehicleType) IN ('compact', 'midsize', 'fullsize', 'suv', 'truck')),
vehicleMake     VARCHAR(15) NOT NULL,
vehicleModel        VARCHAR(15) NOT NULL,
vehicleWhereFrom    VARCHAR(20) NOT NULL CHECK (lower(vehicleWhereFrom) IN ('maryland','virginia','washington, d.c.')),
vehicleWholesaleCost    DECIMAL(9,2)    NOT NULL,
vehicleTradeID      INT);

CREATE TABLE SALES
(saleID         INT     PRIMARY KEY,
grossSalePrice      DECIMAL(9,2),
vehicleStatus       VARCHAR(10) NOT NULL CHECK (lower(vehicleStatus) IN ('available', 'sold', 'pending')),
saleDate        DATE,
saleMileage     INT,
customerID      INT,
salespersonID       INT,
vehicleVIN      VARCHAR(25),
CONSTRAINT SALES_FK1 FOREIGN KEY (customerID) REFERENCES CUSTOMERS(customerID),
CONSTRAINT SALES_FK2 FOREIGN KEY (vehicleVIN) REFERENCES VEHICLES(vehicleVIN));

我已经构建了以下查询来显示参与最多销售的车辆制造商以及所有购买该制造商车辆的人的 customerFullName:

SELECT VEHICLES.vehicleMake, CUSTOMERS.customerFullName
FROM SALES
JOIN CUSTOMERS on SALES.customerID = CUSTOMERS.customerID
    JOIN(
        SELECT SALES.vehicleVIN, VEHICLES.vehicleMake
        FROM SALES
          JOIN VEHICLES ON SALES.vehicleVIN = VEHICLES.vehicleVIN
          GROUP BY SALES.vehicleVIN, VEHICLES.vehicleMake
          HAVING COUNT(SALES.vehicleVIN) >= ALL
          (SELECT COUNT(SALES.vehicleVIN)
             FROM SALES
               INNER JOIN VEHICLES ON SALES.vehicleVIN=VEHICLES.vehicleVIN
               GROUP BY VEHICLES.vehicleMake))
      VEHICLES ON SALES.vehicleVIN = VEHICLES.vehicleVIN
      ORDER BY CUSTOMERS.customerFullName;

当我在我的 Oracle NOVA DB 中运行它时,我只得到“没有选择行”作为结果。我怀疑这是因为以下子句是计算车辆VIN 被出售的次数,而不是计算车辆制造商被出售的次数。

HAVING COUNT(SALES.vehicleVIN) >= ALL
          (SELECT COUNT(SALES.vehicleVIN)
             FROM SALES
               INNER JOIN VEHICLES ON SALES.vehicleVin=VEHICLES.vehicleVIN
               GROUP BY VEHICLES.vehicleMake))
      VEHICLES ON SALES.vehicleVIN = VEHICLES.vehicleVIN
      ORDER BY CUSTOMERS.customerFullName;

我将如何更改此 SELECT Count 子查询以计算车辆发生次数而不是车辆VIN 发生次数?我正在寻找的输出是:

vehicleMake     customerFullName
CAR2            Bob Jim
CAR2            Jim Bob
CAR2            Steve France
CAR2            Tom Williams
CAR2            John Johnson

它应该只显示最畅销的车辆制造商的车辆制造商,以及购买该制造商的任何车辆的人的客户全名。如果您想查看我构建的架构,这里是 SQL Fiddle 的链接:http ://sqlfiddle.com/#!4/b0ac3a

4

1 回答 1

1

http://sqlfiddle.com/#!4/b0ac3a/67

WITH
  tallied_sales AS
(
  SELECT
    SALES.*,
    VEHICLES.vehicleMake,
    COUNT(*) OVER (PARTITION BY VEHICLES.vehicleMake)   AS vehicleMakeSales
  FROM
    SALES
  INNER JOIN
    VEHICLES
      ON VEHICLES.vehicleVIN  = SALES.vehicleVIN
),
  ranked_sales AS
(
  SELECT
    tallied_sales.*,
    RANK() OVER (ORDER BY vehicleMakeSales DESC)   AS vehicleMakeSalesRank
  FROM
    tallied_sales
)
SELECT
  *
FROM
  ranked_sales
INNER JOIN
  CUSTOMERS
    ON CUSTOMERS.customerID = ranked_sales.customerID
WHERE
  ranked_sales.vehicleMakeSalesRank = 1
;
于 2018-02-01T20:05:55.290 回答