查找在 PL/SQL 中在同一条路线上旅行超过三次的乘客

To find passengers travelling more than thrice on the same route in PL/SQL

我在这里创建三个 tables,一个用于存储火车信息。另一个保存乘客信息,另一个保存机票信息。

create table T_Train_Info(
route_no integer primary key,
source varchar2(50),
destination varchar2(50)
)

create table T_Pay_Info(
pax_id integer primary key,
pax_name varchar2(50),
dob date,
gender varchar2(5)
)

create table T_Tkt_Info(
pax_id integer,
route_no integer,
journey_date date,
seat_no varchar2(5),
primary key(pax_id, route_no, journey_date)
)

在 Train_Info table 中,我插入了两条具有相同源和目标的唯一路由,因为相同的源和目标可以有不同的路由。并以相同的方式填充其他 table。在机票table中,我重复了值,因为我的目标是找到在同一条路线上旅行三次的乘客。

insert into T_Train_Info values(1, 'Chennai', 'Pune');
insert into T_Train_Info values(2, 'Chennai', 'Pune');
insert into T_Train_Info values(3, 'Bangalore', 'Kolkata');

insert into T_Tkt_Info values(100, 1, to_date('11/03/2022', 'DD/MM/YYYY'), 22);
insert into T_Tkt_Info values(100, 1, to_date('14/08/2022', 'DD/MM/YYYY'), 23);
insert into T_Tkt_Info values(100, 1, to_date('29/08/2022', 'DD/MM/YYYY'), 24);
insert into T_Tkt_Info values(102, 3, to_date('22/08/2022', 'DD/MM/YYYY'), 24);
insert into T_Tkt_Info values(100, 1, to_date('27/08/2022', 'DD/MM/YYYY'), 24);
insert into T_Tkt_Info values(100, 2, to_date('28/08/2022', 'DD/MM/YYYY'), 24);


insert into T_Pay_Info values(100, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F');
insert into T_Pay_Info values(101, 'B', to_date('23/09/2023', 'DD/MM/YYYY'), 'M');
insert into T_Pay_Info values(102, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F');
insert into T_Pay_Info values(103, 'D', to_date('23/09/2023', 'DD/MM/YYYY'), 'M');
insert into T_Pay_Info values(104, 'A', to_date('11/03/2022', 'DD/MM/YYYY'), 'F');
insert into T_Pay_Info values(105, 'A', to_date('23/09/2023', 'DD/MM/YYYY'), 'M');

这是我的程序,它在 select 语句中不断返回错误 'exact fetch returns more than requested number of rows'。我在这里做错了什么?

create or replace procedure pr_pass_route_details(x in T_Train_Info.Source%type, y in T_Train_Info.Destination%type) is
  pr_name T_Pay_Info.Pax_Name%type;
begin
  for i in (select pax_id from t_tkt_info group by pax_id,route_no having count(*) >=3) loop
    select pax_name into pr_name from t_pay_info where pax_id = i.pax_id and T_Train_Info.Source=x and T_Train_Info.Destination=y;

    dbms_output.put_line(pr_name);

  end loop;

end pr_pass_route_details;

我不确定你为什么要写一个 SP 来做这个,因为你可以通过一个简单的查询来实现:

SELECT
pax_id,
route_no,
COUNT(journey_date)
FROM T_Tkt_Info
GROUP BY 
pax_id,
route_no
HAVING COUNT(journey_date) = 3