[英]SQL Query for Counting Distinct Values From One Column When More Than 1 Distinct Value exists in Another Column in the same table
I have a single table called "Vehicle" that contains a column with unique VehicleId's. 我有一个名为“ Vehicle”的表,其中包含带有唯一VehicleId的列。 On this same "Vehicle" table I also have a column for QuoteId's. 在同一张“车辆”表上,我还为QuoteId列。
I need to create a SQL query that returns how many distinct QuoteId's were obtained with more than one distinct VehicleId. 我需要创建一个SQL查询,该查询返回使用多个不重复的VehicleId获得多少不重复的QuoteId。 In other words, I'm trying to determine a count of how many quotes had more than one vehicle on them. 换句话说,我正在尝试确定有多少报价引用了一个以上的车辆。
I have searched all over for this information and come up with a basic text statement to try and help me figure this out: 我到处搜索了这些信息,并提出了一个基本的文本声明,以尝试帮助我解决这一问题:
"Select the count of distinct QuoteId's that have multiple distinct VehicleId's" “选择具有多个不同VehicleId的不同QuoteId的计数”
I am unable to come up with a way to get this to work but have included an example of what I am attempting to accomplish to try and clarify: 我无法提出一种使它起作用的方法,但是提供了一个示例,说明了我正在尝试完成的工作以进行澄清:
SELECT COUNT(DISTINCT QuoteId's) AS 'Multi Vehicle Quotes'
From Vehicle
WHERE VehicleId = DISTINCT VehicleId > 1
Any help or advice would be greatly appreciated! 任何帮助或建议,将不胜感激!
I think you are very close there. 我想你在那里很近。 You need to first find quotes with more than one vehicle and store that in a CTE. 您首先需要查找不止一种车辆的报价并将其存储在CTE中。 Then count the number of quotes from the CTE. 然后计算CTE的报价数量。
With CTE as (
SELECT (
VehicleID
,COUNT(DISTINCT Quotes)
FROM Vehicle
HAVING COUNT(DISTINCT Quotes) > 1)
SELECT COUNT(VehicleID) as MultiVehicleQuotes
FROM CTE
You need nested aggregation: 您需要嵌套聚合:
select count(*)
from
(
SELECT QuoteId
From Vehicle
group by QuoteId
having count(DISTINCT VehicleId) > 1
) as dt
Instead of COUNT(DISTINCT) > 1
which is expensive you can also use 您也可以使用COUNT(DISTINCT) > 1
代替昂贵的价格
having min(VehicleId) <> max(VehicleId)
这是一个查询,它将帮助您实现所需的目标。
SELECT DISTINCT vehicleId, qouteId,count(qouteId) as vehicleCountForQouteId FROM Vehicle GROUP BY qouteId
Let's assume this is the data on your vechicle
table: 假设这是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
Your first step would be to get the list of quote_id
, and how many there are of each. 第一步是获取quote_id
的列表,以及每个列表的数目。 Since, in the previous table, vehicle_id
is unique, you just need: 由于在上表中, 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
Second step : from the previous query, you want only those with number_of_vehicles_quoted
> 1. That can be done with a HAVING
clause after the GROUP BY
, which places a further restriction on the GROUP
ed rows. 第二步 :从上一个查询中,只需要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
If you don't like HAVING
, or don't feel comfortable with it, you can wrap the query with another one, and do: 如果您不喜欢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 ;
Third step : finally, count(*) the number of rows of the previous query: 第三步 :最后,对先前查询的行数进行计数(*):
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 |
You can check the whole setup and the steps at dbfiddle here . 您可以检查整个安装和dbfiddle的步骤这里 。 The query is plain SQL, and works with all engines available on DBFiddle (except Oracle, which complains about identifiers being too long, and that would work if I weren't so verbose ;-) 该查询是简单的SQL,并且可以与DBFiddle上所有可用的引擎一起使用(Oracle除外,Oracle抱怨标识符太长,如果我不那么冗长,那会起作用;
NOTE 1: you can simplify a bit the last query, since you don't use some of the information on the outermost query. 注意1:您可以简化最后一个查询,因为您没有使用最外层查询中的某些信息。 That will speed it up a bit, although not in a significant way: 尽管不会显着提高速度,但可以加快速度:
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 ;
NOTE 2: If your vehicle_id
s were not guaranteed to be unique, you'd use: 注意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 ;
Here, try this: 在这里,尝试一下:
SELECT COUNT(a.QuoteID) AS 'Count',
a.QuoteID AS 'QuoteID'
FROM Problems.Vehicle AS a
GROUP BY a.QuoteID
HAVING COUNT(a.QuoteID) > 1
NOTE: Problems.Vehicle is the name of the schema the table is in and the name of the table. 注意:Problems.Vehicle是表所在的架构的名称和表的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.