获取用户最常使用其乘车服务的乘车服务提供商详细信息
Fetch the ride provider details whose rides were mostly used by the users
我必须获取向用户提供乘车次数最多的乘车服务提供商的详细信息。
user_detail table 中的 user_id 与 RIDE table 中的 ride_provider_id 相同。
我试过了,但在最后一行出现错误 'ORA-00920: invalid relational operator';
select u.*
from user_details u,
(select ride_provider_id,count(ride_provider_id) as of_ride
from ride
group by ride_provider_id) r2
where u.user_id= r2.ride_provider_id
having max(r2.of_ride);
在 Oracle 中,您可以使用 order by
和 fetch first
子句来表达:
select u.*
from user_details u join
(select ride_provider_id, count(*) as of_ride
from ride
group by ride_provider_id
) r
on u.user_id = r.ride_provider_id
order by of_ride desc
fetch first 1 row only;
但是,这似乎效率很低。如果你使用的是子查询的计数,那么查询可以在子查询中做限制:
select u.*
from user_details u join
(select ride_provider_id, count(*) as of_ride
from ride
group by ride_provider_id
order by count(*) desc
fetch first 1 row only
) r
on u.user_id = r.ride_provider_id
我必须获取向用户提供乘车次数最多的乘车服务提供商的详细信息。
user_detail table 中的 user_id 与 RIDE table 中的 ride_provider_id 相同。
我试过了,但在最后一行出现错误 'ORA-00920: invalid relational operator';
select u.*
from user_details u,
(select ride_provider_id,count(ride_provider_id) as of_ride
from ride
group by ride_provider_id) r2
where u.user_id= r2.ride_provider_id
having max(r2.of_ride);
在 Oracle 中,您可以使用 order by
和 fetch first
子句来表达:
select u.*
from user_details u join
(select ride_provider_id, count(*) as of_ride
from ride
group by ride_provider_id
) r
on u.user_id = r.ride_provider_id
order by of_ride desc
fetch first 1 row only;
但是,这似乎效率很低。如果你使用的是子查询的计数,那么查询可以在子查询中做限制:
select u.*
from user_details u join
(select ride_provider_id, count(*) as of_ride
from ride
group by ride_provider_id
order by count(*) desc
fetch first 1 row only
) r
on u.user_id = r.ride_provider_id