我有以下表格:
CREATE TABLE CUSTOMERS
(customerID INT PRIMARY KEY,
customerFirName VARCHAR(20) NOT NULL,
customerLasName VARCHAR(20) NOT NULL,
customerMiName VARCHAR(1) 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 DISTINCT v.VEHICLEMAKE, COUNT(*) OVER (PARTITION BY s.VEHICLEVIN) "SALES_BY_MAKE", c.CUSTOMERFULLNAME, COUNT(*) OVER (PARTITION BY c.CUSTOMERZIP)"CUSTOMERS"
FROM SALES s, VEHICLES v, CUSTOMERS c
WHERE s. VEHICLEVIN = v. VEHICLEVIN
and c. CUSTOMERID = s. CUSTOMERID
ORDER BY 2 DESC;
这是我得到的输出:
VEHICLEMAKE SALES_BY_MAKE CUSTOMERFULLNAME CUSTOMERS
Chevrolet 11 Adam E Whitney 2
Chevrolet 11 Alberto L Ross 2
Chevrolet 11 Alexis T Moon 2
Chevrolet 11 Finley H Tritt 2
Chevrolet 11 Jayda V Rush 2
Chevrolet 11 Junior E Hanes 2
Chevrolet 11 Kamari H Webster 1
Chevrolet 11 Linda L Lawrence 2
Chevrolet 11 Luke E Boyer 4
Chevrolet 11 Samantha D Holden 2
Chevrolet 11 Sydnee B Herman 2
我需要输出如下所示:
VEHICLEMAKE SALES_BY_MAKE CUSTOMERFULLNAME
Chevrolet 11 Adam E Whitney
Alberto L Ross
Alexis T Moon
Finley H Tritt
Jayda V Rush
Junior E Hanes
Kamari H Webster
Linda L Lawrence
Luke E Boyer
Samantha D Holden
Sydnee B Herman
如图所示,CUSTOMERS列不是必需的,但当我删除它时,我收到错误。也许我对DISTINCT的方法一无所知?
基本上,我只需要一个单一选择查询,显示列出的车辆的制造一次,列出的销售数量,然后是购买该车辆的客户列表。它还应该说明两辆或两辆以上车辆销量最高的可能性,例如,如果雪佛兰和福特都有11个销售,他们都应该出现在名单上,以及购买它们的客户名称。
我希望我没有把它弄得太混乱。谢谢你的帮助!
答案 0 :(得分:1)
从不在FROM
子句中使用逗号。 始终使用正确的JOIN
语法。
SELECT vc.*
FROM (SELECT vc.*, MAX(SALES_BY_MAKE) OVER () as MAX_SALES_BY_MAKE
FROM (SELECT v.VEHICLEMAKE, c.CUSTOMERFULLNAME,
COUNT(*) OVER (PARTITION BY s.VEHICLEMAKE) as SALES_BY_MAKE
FROM SALES s JOIN
VEHICLES v
ON s.VEHICLEVIN = v.VEHICLEVIN JOIN
CUSTOMERS c
ON c.CUSTOMERID = s.CUSTOMERID
) vc
) vc
WHERE SALES_BY_MAKE = MAX_SALES_BY_MAKE
ORDER BY VEHICLEMAKE;
答案 1 :(得分:0)
如果是SQL * Plus,您可以使用其BREAK
命令来产生这样的效果。如今人们很少使用它,但谁知道 - 你(或其他人)可能需要它。我希望你不会介意我发布它。
SQL> break on job_id on cnt
SQL>
SQL> select job_id,
2 count(*) over (partition by job_id) cnt,
3 last_name
4 From employees
5 where job_id between 'AD_VP' and 'MK_MAN'
6 order by job_id, last_name;
JOB_ID CNT LAST_NAME
---------- ---------- -------------------------
AD_VP 2 De Haan
Kochhar
FI_ACCOUNT 5 Chen
Faviet
Popp
Sciarra
Urman
FI_MGR 1 Greenberg
HR_REP 1 Mavris
IT_PROG 5 Austin
Ernst
Hunold
Lorentz
Pataballa
MK_MAN 1 Hartstein
15 rows selected.
SQL>
答案 2 :(得分:0)
想出来;这很有效。
WITH
vehicleMakeCount AS
(
SELECT
SALES.saleID, SALES.customerID, SALES.vehicleVIN, VEHICLES.vehicleMake,
COUNT(*) OVER (PARTITION BY VEHICLES.vehicleMake) AS salesByMake
FROM
SALES
INNER JOIN
VEHICLES ON VEHICLES.vehicleVIN = SALES.vehicleVIN
),
salesRank AS
(
SELECT
vehicleMakeCount.*,
RANK() OVER (ORDER BY salesByMake DESC) AS vehicleMakeSalesRank
FROM
vehicleMakeCount
)
SELECT CUSTOMERS.customerFullName, salesRank.vehicleMake
FROM
salesRank
INNER JOIN
CUSTOMERS
ON CUSTOMERS.customerID = salesRank.customerID
WHERE
salesRank.vehicleMakeSalesRank = 1
ORDER BY salesRank.vehicleMake;