将两个SQL ORACLE选择语句与JOIN和COUNT组合在一起

时间:2018-01-31 00:06:10

标签: sql oracle join count

我已经开发了两个查询,这些查询来自我的数据库中的单独表。第一个查询是:

SELECT VEHICLES.vehicleModel, 
COUNT(*) SALES_BY_MODEL
FROM SALES
INNER JOIN VEHICLES ON SALES.vehicleVIN = VEHICLES.vehicleVIN
GROUP BY VEHICLES.vehicleModel
ORDER BY SALES_BY_MODEL DESC;

它的输出如下:

VEHICLEMODEL    SALES_BY_MODEL
spark                        9
accord                       8
focus                        6
xt5                          5
rx                           5
ranger                       5
impala                       5

第二个查询是:

SELECT CUSTOMERS.customerZip,
COUNT(*) SALES_BY_ZIP
FROM SALES
INNER JOIN CUSTOMERS ON SALES.customerID = CUSTOMERS.customerID
GROUP BY CUSTOMERS.customerZip
ORDER BY SALES_BY_ZIP DESC;

它的输出如下:

 CUSTOMERZIP     SALES_BY_ZIP
 89523                      6
 04019                      4
 85001                      4
 03038                      4
 13760                      4
 20886                      3

我需要将这两个查询合并到一个 SELECT 语句中。我试过这个,但是我在 COUNT 功能上遇到了一些错误:

SELECT VEHICLES.vehicleModel, CUSTOMERS.customerZip,
COUNT(*) SALES_BY_MODEL, SALES_BY_ZIP
FROM SALES
INNER JOIN VEHICLES ON SALES.vehicleVIN = VEHICLES.vehicleVIN
INNER JOIN CUSTOMERS ON SALES.customerID = CUSTOMERS.customerID
GROUP BY VEHICLES.vehicleModel, CUSTOMERS.customerZip
ORDER BY SALES_BY_MODEL DESC, SALES_BY_ZIP DESC;

所以我接着尝试了这个,但现在我的计数已经结束了:

SELECT VEHICLES.vehicleModel, CUSTOMERS.customerZip,
(SELECT COUNT(*) AS SALES_BY_MODEL
    FROM
        (SELECT DISTINCT vehicleVIN FROM SALES
        )
) AS SALES_BY_MODEL,
(SELECT COUNT(*) AS SALES_BY_ZIP
    FROM
        (SELECT DISTINCT customerZip FROM CUSTOMERS
        )
) AS SALES_BY_ZIP
FROM SALES
INNER JOIN VEHICLES ON SALES.vehicleVin = VEHICLES.vehicleVIN
INNER JOIN CUSTOMERS ON SALES.customerID = CUSTOMERS.customerID
GROUP BY VEHICLES.vehicleModel, CUSTOMERS.customerZip;

输出应该类似于:

VEHICLEMODEL    SALES_BY_MODEL     
escalade                   20       
m3                         15      
spark                      14                
silverado                  13     
durango                    12     
accord                     10     
accent                     9       
sedan deville              2       

CUSTOMERZIP     SALES_BY_ZIP
60068                     10
12550                      9
48045                      8
28115                      7 

将这两个查询组合成一个查询的“正确”方法是什么,因此它们在单个表中显示为输出。 SALES_BY_MODEL SALES_BY_ZIP 列应按降序排序,但 SALES_BY_MODEL SALES_BY_ZIP 列不相互关联因此,将它们保存在同一输出表中时没有负面影响。

谢谢!

2 个答案:

答案 0 :(得分:0)

您可以像上面提到的那样获得输出,但是您不会像在预期结果中显示的那样在两个表之间获取列名。一种解决方法是将类型作为另一列。

SELECT * FROM
(SELECT "VEHICLEMODEL" TYPE,
    VEHICLES.vehicleModel, 
    COUNT(*) COUNTS
FROM SALES
INNER JOIN VEHICLES ON SALES.vehicleVIN = VEHICLES.vehicleVIN
GROUP BY VEHICLES.vehicleModel
UNION
SELECT "CUSTOMERZIP" TYPE,
    CUSTOMERS.customerZip,
    COUNT(*)
FROM SALES
INNER JOIN CUSTOMERS ON SALES.customerID = CUSTOMERS.customerID
GROUP BY CUSTOMERS.customerZip
) CS
ORDER BY CS.TYPE DESC, CS.COUNTS DESC

答案 1 :(得分:0)

结束了解决这个问题并且效果很好。感谢所有的帮助!

WITH
  modelSales AS(
SELECT VEHICLES.vehicleModel, COUNT(*) AS salesByModel, ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) AS rankID
  FROM SALES
    INNER JOIN VEHICLES ON SALES.vehicleVIN = VEHICLES.vehicleVIN
    GROUP BY VEHICLES.vehicleModel),

  zipSales AS(
SELECT CUSTOMERS.customerZip, COUNT(*) AS salesByZip, ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) AS rankID
  FROM SALES
    INNER JOIN CUSTOMERS ON SALES.customerID= CUSTOMERS.customerID
    GROUP BY CUSTOMERS.customerZip)

  SELECT m.vehicleModel, m.salesByModel, z.customerZip, z.salesByZip
FROM modelSales m
  FULL OUTER JOIN zipSales z ON m.rankID = z.rankID
  ORDER BY COALESCE(m.rankID, z.rankID);