[英]SQL Query for Counting Distinct Values From One Column When More Than 1 Distinct Value exists in Another Column in the same table
我有一个名为“ Vehicle”的表,其中包含带有唯一VehicleId的列。 在同一张“车辆”表上,我还为QuoteId列。
我需要创建一个SQL查询,该查询返回使用多个不重复的VehicleId获得多少不重复的QuoteId。 换句话说,我正在尝试确定有多少报价引用了一个以上的车辆。
我到处搜索了这些信息,并提出了一个基本的文本声明,以尝试帮助我解决这一问题:
“选择具有多个不同VehicleId的不同QuoteId的计数”
我无法提出一种使它起作用的方法,但是提供了一个示例,说明了我正在尝试完成的工作以进行澄清:
SELECT COUNT(DISTINCT QuoteId's) AS 'Multi Vehicle Quotes'
From Vehicle
WHERE VehicleId = DISTINCT VehicleId > 1
任何帮助或建议,将不胜感激!
我想你在那里很近。 您首先需要查找不止一种车辆的报价并将其存储在CTE中。 然后计算CTE的报价数量。
With CTE as (
SELECT (
VehicleID
,COUNT(DISTINCT Quotes)
FROM Vehicle
HAVING COUNT(DISTINCT Quotes) > 1)
SELECT COUNT(VehicleID) as MultiVehicleQuotes
FROM CTE
您需要嵌套聚合:
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)
这是一个查询,它将帮助您实现所需的目标。
SELECT DISTINCT vehicleId, qouteId,count(qouteId) as vehicleCountForQouteId FROM Vehicle GROUP BY qouteId
假设这是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的步骤这里 。 该查询是简单的SQL,并且可以与DBFiddle上所有可用的引擎一起使用(Oracle除外,Oracle抱怨标识符太长,如果我不那么冗长,那会起作用;
注意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 在这里
注意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 ;
在这里,尝试一下:
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是表所在的架构的名称和表的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.