2017-07-21 133 views
0

我有一个名为“Vehicle”的表,其中包含具有唯一VehicleId的列。在同样的“Vehicle”表中,我也有一个QuoteId列。用于在同一个表中的另一列中存在多于一个不同值的情况下计算来自一列的不同值的SQL查询

我需要创建一个SQL查询,返回多个不同的QuoteId与多个不同的VehicleId获得。换句话说,我试图确定有多少报价有超过一辆车的数量。

我已经找遍了这一信息,并拿出一个基本的文字声明,试图帮我想出解决办法:

“选择不同的QuoteId的具有多个不同的VehicleId年代数”

我无法想出一个办法来得到这个工作,但已经有包括什么,我试图完成的尝试和澄清的一个例子:

SELECT COUNT(DISTINCT QuoteId's) AS 'Multi Vehicle Quotes' 
From Vehicle 
WHERE VehicleId = DISTINCT VehicleId > 1 

任何帮助或建议将不胜感激!

+0

@joanolo:但解决方案只是基本的标准SQL,它运行在每个DBMS上,我怀疑是否有更好/更差的执行解决方案。 – dnoeth

回答

0

我觉得你非常接近那里。您需要首先查找多个车辆的报价并将其存储在CTE中。然后计算来自CTE的报价数量。

With CTE as (
SELECT (
VehicleID 
,COUNT(DISTINCT Quotes) 
FROM Vehicle 
HAVING COUNT(DISTINCT Quotes) > 1) 

SELECT COUNT(VehicleID) as MultiVehicleQuotes 
FROM CTE 
0

您需要嵌套的聚合:

select count(*) 
from 
(
    SELECT QuoteId 
    From Vehicle 
    group by QuoteId 
    having count(DISTINCT VehicleId) > 1 
) as dt 

代替COUNT(DISTINCT) > 1这是昂贵的,你也可以使用

having min(VehicleId) <> max(VehicleId) 
0

下面是一个查询,将帮助你实现你想要的..

SELECT DISTINCT vehicleId, qouteId,count(qouteId) as vehicleCountForQouteId FROM Vehicle GROUP BY qouteId 
0

假设这是你的vechicle表中的数据:

SELECT * FROM vehicle ORDER BY vehicle_id ; 
 
vehicle_id | quote_id 
---------: | -------: 
     1 |  100 
     2 |  100 
     3 |  100 
     4 |  101 
     5 |  102 
     6 |  102 
     7 |  103 

第一步将得到quote_id名单,多少也有各自的。因为,在上表中,vehicle_id是独一无二的,你只需要:

SELECT 
    quote_id, count(*) AS number_of_vehicles_quoted 
FROM 
    vehicle 
GROUP BY 
    quote_id ; 
 
quote_id | number_of_vehicles_quoted 
-------: | ------------------------: 
    100 |       3 
    101 |       1 
    102 |       2 
    103 |       1 

第二步:从以前的查询,你只希望那些number_of_vehicles_quoted> 1 这是可以做到的在GROUP BY之后的HAVING子句,其对GROUP ed行进一步限制。

SELECT 
    quote_id, 
    count(*) AS number_of_vehicles_quoted 
FROM 
    vehicle 
GROUP BY 
    quote_id 
HAVING 
    count(*) > 1 ; 
 
quote_id | number_of_vehicles_quoted 
-------: | ------------------------: 
    100 |       3 
    102 |       2 

如果你不喜欢HAVING,或感觉不舒服,你可以用另外一个包裹查询,然后执行:

SELECT 
    quote_id, number_of_vehicles_quoted 
FROM 
    (SELECT 
     quote_id, 
     count(*) AS number_of_vehicles_quoted 
    FROM 
     vehicle 
    GROUP BY 
     quote_id 
    ) AS q 
WHERE 
    number_of_vehicles_quoted > 1 ; 

第三步 :最后,计算(*)先前查询的行数:

SELECT 
    count(*) AS count_of_quotes_with_more_than_one_vehicle_id 
FROM 
    (SELECT 
     quote_id, 
     count(*) AS number_of_vehicles_quoted 
    FROM 
     vehicle 
    GROUP BY 
     quote_id 
    HAVING 
     count(*) > 1 
    ) AS quotes_with_more_than_one_vehicle_id ; 
 
| count_of_quotes_with_more_than_one_vehicle_id | 
| --------------------------------------------: | 
|            2 | 

您可以检查整个设置和步骤dbfiddle here。查询是普通的SQL,并与所有引擎的工作原理可在DBFiddle(除了甲骨文,它抱怨标识符太长了,如果我不那么冗长;-)


注1,将工作:您可以简化最后一个查询,因为您不使用最外层查询中的某些信息。这会加速它的一点,虽然不以显著方式:

SELECT 
    count(*) AS count_of_quotes_with_more_than_one_vehicle_id 
FROM 
    (SELECT 
     -- quote_id, -- You actually don't use this one in the outer query 
     -- count(*) AS number_of_vehicles_quoted -- This neither 
     1 
    FROM 
     vehicle 
    GROUP BY 
     quote_id 
    HAVING 
     count(*) > 1 
    ) AS quotes_with_more_than_one_vehicle_id ; 

dbfiddle here


注2:如果您的vehicle_id小号都不能保证是独一无二的,你会使用方法:

SELECT 
    count(*) AS count_of_quotes_with_more_than_one_vehicle_id 
FROM 
    (SELECT 
     1 
    FROM 
     vehicle 
    GROUP BY 
     quote_id 
    HAVING 
     count(DISTINCT vehicle_id) > 1 
    ) AS quotes_with_more_than_one_vehicle_id ; 
0

这里,试试这个:

SELECT COUNT(a.QuoteID) AS 'Count', 
      a.QuoteID   AS 'QuoteID' 
FROM  Problems.Vehicle AS a 
GROUP BY a.QuoteID 
HAVING COUNT(a.QuoteID) > 1 

注意:Problems.Vehicle是表所在的模式的名称和表的名称。

相关问题