2017-07-21 58 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是表所在的模式的名稱和表的名稱。

相關問題