SQL Select 带连接的查询:如何在没有外键的情况下计算非主属性的特定出现次数
SQL Select Query with Join: How to Count a Certain Number of Occurences on non-primary Attribute without a Foreign Key
使用下表:
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));
我构建了以下查询以显示参与最多销售的车辆品牌以及所有购买该品牌车辆的客户的全名:
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 中 运行 时,我只得到 "no rows selected" 作为结果。我怀疑这是因为以下子句计算的是 vehicleVIN 的销售次数,而不是计算 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;
我如何更改此 SELECT Count 子查询以计算 vehicleMake 的出现次数而不是 vehicleVIN 的出现次数?我正在寻找的输出是:
vehicleMake customerFullName
CAR2 Bob Jim
CAR2 Jim Bob
CAR2 Steve France
CAR2 Tom Williams
CAR2 John Johnson
它应该只显示销售最多的车辆品牌的车辆品牌,以及购买该品牌任何车辆的人的客户全名。这是 link 到 SQL Fiddle 如果您想查看我构建的架构:http://sqlfiddle.com/#!4/b0ac3a
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
;
使用下表:
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));
我构建了以下查询以显示参与最多销售的车辆品牌以及所有购买该品牌车辆的客户的全名:
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 中 运行 时,我只得到 "no rows selected" 作为结果。我怀疑这是因为以下子句计算的是 vehicleVIN 的销售次数,而不是计算 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;
我如何更改此 SELECT Count 子查询以计算 vehicleMake 的出现次数而不是 vehicleVIN 的出现次数?我正在寻找的输出是:
vehicleMake customerFullName
CAR2 Bob Jim
CAR2 Jim Bob
CAR2 Steve France
CAR2 Tom Williams
CAR2 John Johnson
它应该只显示销售最多的车辆品牌的车辆品牌,以及购买该品牌任何车辆的人的客户全名。这是 link 到 SQL Fiddle 如果您想查看我构建的架构:http://sqlfiddle.com/#!4/b0ac3a
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
;