我有一个名为“Vehicle”的表,其中包含一个具有唯一VehicleId的列。在同一个“车辆”表中,我还有一个QuoteId的列。
我需要创建一个SQL查询,该查询返回使用多个不同的VehicleId获得多少个不同的QuoteId。换句话说,我正在尝试确定有多少报价在其上有多个车辆的计数。
我已经搜索了所有这些信息,并提出了一个基本的文本声明,试图帮助我解决这个问题:
“选择具有多个不同VehicleId”的不同QuoteId的计数“
我无法想出办法让这个工作起作用,但已经包含了一个我想要尝试澄清的例子:
SELECT COUNT(DISTINCT QuoteId's) AS 'Multi Vehicle Quotes'
From Vehicle
WHERE VehicleId = DISTINCT VehicleId > 1
非常感谢任何帮助或建议!
答案 0 :(得分: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
答案 1 :(得分: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)
答案 2 :(得分:0)
这是一个可以帮助您实现所需目标的查询。
SELECT DISTINCT vehicleId, qouteId,count(qouteId) as vehicleCountForQouteId FROM Vehicle GROUP BY qouteId
答案 3 :(得分: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
&gt; 1。
这可以通过HAVING
之后的GROUP BY
子句来完成,这会对GROUP
行进行进一步的限制。
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上可用的所有引擎(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 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 ;
答案 4 :(得分: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是表所在架构的名称和表的名称。