使用JOIN

时间:2018-01-25 01:25:29

标签: sql oracle join max

我有三个表:客户,车辆和销售。我需要一个SELECT语句,它将形成一个包含三列的输出:VEHICLEMAKE,SALESBYMAKE和CUSTOMERFULLNAME。输出应类似于以下内容:

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 

该列表应仅包含SALESBYMAKE的MAX数,除非MAX绑定,否则它还包含VEHICLEMAKE,SALESBYMAKE的数量和其他VEHICLEMAKE的CUSTOMERFULLNAME。我已经工作了几个小时,绝对没有用!以下是表,仅包含必要的属性。

CREATE TABLE CUSTOMERS
(customerID     INT     PRIMARY KEY,
customerFirName     VARCHAR(20) NOT NULL,
customerLasName     VARCHAR(20) NOT NULL,
customerMiName      VARCHAR(1)  NOT NULL;

CREATE TABLE VEHICLES
(vehicleVIN     VARCHAR(25) PRIMARY KEY,
vehicleMake     VARCHAR(15) NOT NULL;

CREATE TABLE SALES
(saleID         INT     PRIMARY KEY,
customerID      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;

问题是,这给了我整个列表,而不是MAX值。我不确切知道在哪里或如何实施MAX语句只包括最多购买的车辆制造商。

我也按照我提出的另一个人的建议尝试了这个,但是对它没有任何意义,并且没有错误执行。

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.VEHICLEVIN) 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;

我认为我尝试的第一个是正确的降低路径,因为它确实提供了我需要的列表,但它不是仅选择MAX值,而是提供所有值和从最高到最低排序。我考虑过添加一个LIMIT,但这不会允许“平局”。

我真的只需要指导在何处/如何实现MAXcount以获得最高输出。

2 个答案:

答案 0 :(得分:0)

尝试此查询,只是它会重复前2列的值

SELECT  C.vehicleMake,C.nCount,B.customerFirName + ' ' + B.customerMiName     + ' ' + B.customerLasName cFullName
FROM SALES A
JOIN CUSTOMERS B ON A.customerID = B.customerID
JOIN    (
    SELECT A.vehicleVIN,B.vehicleMake,COUNT(*) nCount
    FROM SALES
    JOIN VEHICLES B ON A.vehicleVIN = B.vehicleVIN
    GROUP BY A.vehicleVIN,B.vehicleMake
    )C ON A.vehicleVIN = C.vehicleVIN

答案 1 :(得分: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;