微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQL 不使用 ALIAS 列进行计算

如何解决SQL 不使用 ALIAS 列进行计算

问题陈述 - 从出租车服务的给定行程和用户表中,编写查询以返回 10 月份前两天的取消率,四舍五入到小数点后两位,对于不涉及被禁止的乘客或司机的行程。

关于 Oracle sql 的问题代码

create table trips (trip_id int,rider_id int,driver_id int,status varchar2(200),request_date date);
insert into trips values (1,1,10,'completed',to_date ('2020-10-01','YYYY/MM/DD'));
insert into trips values (2,2,11,'cancelled_by_driver','YYYY/MM/DD'));
insert into trips values (3,3,12,'YYYY/MM/DD'));
insert into trips values (4,4,to_date ('2020-10-02','YYYY/MM/DD'));
insert into trips values (5,'YYYY/MM/DD'));
insert into trips values (6,'YYYY/MM/DD'));
insert into trips values (7,to_date ('2020-10-03','YYYY/MM/DD'));

create table users (user_id int,banned varchar2(200),type varchar2(200));
insert into users values (1,'no','rider');
insert into users values (2,'yes','rider');
insert into users values (3,'rider');
insert into users values (4,'rider');
insert into users values (10,'driver');
insert into users values (11,'driver');
insert into users values (12,'driver');

我的解决方代码如下。但是,我收到以下错误。有人可以帮忙吗?

ORA-00904: "TOTAL_TRIPS": invalid identifier

解决方代码

select request_date,(1-(trips_completed/total_trips)) as "cancel_rate"
from
((
select request_date,sum(case when status = 'completed' then 1 else 0 end) as "trips_completed",sum(case when status = 'cancelled_by_driver' then 1 else 0 end) as "trips_cancelled",sum(case when status = 'cancelled_by_driver' then 1 when status= 'completed' then 1 else 0 end) as "total_trips"
from 
(
select t.rider_id,t.driver_id,t.status,t.request_date,u.banned as "not_banned_rider",u.banned as "not_banned_driver"
from trips t
join users u
on t.rider_id=u.user_id
where u.banned='no'
)
group by request_date
having request_date <> to_date ('2020-10-03','YYYY/MM/DD')
));

解决方法

首先,不要将标识符放在双引号中。它们只会使查询变得混乱。

其他一些需要解决的问题:

  • 不需要两级子查询。
  • 学习使用正确的 date 字面语法。
  • 我认为您想要的是 < 而不是 <>

所以这表明:

select request_date,(1-(trips_completed/total_trips)) as cancel_rate
from (select request_date,sum(case when status = 'completed' then 1 else 0 end) as trips_completed,sum(case when status = 'cancelled_by_driver' then 1 else 0 end) as trips_cancelled,sum(case when status = 'cancelled_by_driver' then 1 when status = 'completed' then 1 else 0 end) as total_trips
      from trips t join
           users u
           on t.rider_id = u.user_id
      where u.banned = 'no' and
            t.request_date < date '2020-10-03'
      group by request_date 
     ) rd;

这可以使用 avg() 进一步简化:

select request_date,avg(case when status = 'completed' then 1 else 0 end) as cancel_rate
from trips t join
     users u
     on t.rider_id = u.user_id
where u.banned = 'no' and
      request_date < date '2020-10-03'
group by request_date ;

注意:这解决了在您的问题中修复查询的问题。它实际上并没有正确回答问题,原因如下:

  • 我很确定这个问题需要一个取消率,而不是两个日期一个。
  • 它不考虑被禁止的司机。
  • 我不确定如何处理“被用户取消”。
,

ORA-00904:“TOTAL_TRIPS”:标识符无效

仅表示“total_trips”所写的内容无效

只需使用 total_trips(不带引号)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。