简体   繁体   中英

SQL join query -- two tables

I have two tables buses and passengers:

create table buses (id
integer primary key, origin varchar not null, destination varchar not null, time varchar not null, unique
(origin, destination, time) ); 
create table passengers ( id integer primary key, origin varchar not null,
destination varchar not null, time varchar not null );
insert into buses (id, origin, destination, time) values (10, 'Warsaw', 'Berlin' , '10:55');
insert into buses (id, origin, destination, time) values (20,  'Berlin' , 'Paris' , '06:20');
insert into buses (id, origin, destination, time) values (21,  'Berlin' , 'Paris' , '14:00');
insert into buses (id, origin, destination, time) values (22,  'Berlin' , 'Paris' , '21:40');
insert into buses (id, origin, destination, time) values (30, 'Paris', 'Madrid' , '13:30');

insert into passengers (id, origin, destination, time) values (1 , 'Paris' , 'Madrid' , '13:30');
insert into passengers (id, origin, destination, time) values (2 , 'Paris' , 'Madrid' , '13:31');
insert into passengers (id, origin, destination, time) values (10 , 'Warsaw', 'Paris' , '10:00');
insert into passengers (id, origin, destination, time) values (11 , 'Warsaw', 'Berlin', '22:31');
insert into passengers (id, origin, destination, time) values (40 , 'Berlin', 'Paris' , '06:15');
insert into passengers (id, origin, destination, time) values (41 , 'Berlin', 'Paris' , '06:50');
insert into passengers (id, origin, destination, time) values (42 , 'Berlin', 'Paris' , '07:12');
insert into passengers (id, origin, destination, time) values (43 , 'Berlin', 'Paris' , '12:03');
insert into passengers (id, origin, destination, time) values (44 , 'Berlin', 'Paris' , '20:00');

For each bus, I want to return the number of passengers boarding it. Just two columns the id and passengers on board.

10 -> 0 20 -> 1 21 -> 3 22 -> 1 30 -> 1

Explanation: -- Bus id: 10 moves from Warsaw to Berlin and departs at 10:55 but a passenger-only boarded once at 22:31. So, no passengers have boarded the bus: 10 and hence 0 passengers. -- Bus id: 20 from Berlin to Paris starts at 06:20 and only one passenger has boarded it at 06:15. So, 1 passenger has boarded the bus: 20.

The join condition would be multiple columns of origin and destination I guess and I am not sure how to handle the time column and specify the conditions and write the WHERE clause

```
SELECT
  b.id, count(*),
FROM buses b
JOIN passenger p
  ON p.origin=b.origin
    AND p.destination=e.destination; 
```

This question and query needs some refinement. I think that the bus is travelling and hence there should be a depart time and arrival time. The same should be in the other table as well. Then you can count the number of passengers in that time. However, the query you wrote also had some errors and if you want the exact time match then here is the below mentioned query

SELECT B.id, COUNT(*)
FROM buses b
JOIN passengers p
ON p.origin=b.origin
AND p.destination=b.destination
AND p.time=b.time; 

Try this and let me know

我认为如果将列时间的变量类型更改为日期时间,则可以使用此查询比较时间并获得输出,

SELECT b.id, COUNT(*) FROM buses b JOIN passengers p ON p.origin = b=origin AND p.destination = b.destination and p.time > b.time

Use this:

Select b.id,isnull(Count(p.id),0) as Total from buses b ,passengers p where b.id=p.id
    and b.time >=p.time 
    group by p.id, b.id

select bus1.id, (select count(passengers.id) from customers where bus1.origin =passengers.origin and bus1.destination=passengers.destination andpassengers.time<=buses1.time and ifnull((select bus2.time from bus as bus2 where bus2.origin= bus1.origin and bus2.destination=buses1.destination and bus2.time<buses1.time order by bus2.time desc limit 1),'00:00')<passengers.time) as count from巴士 as bus1 按 bus1.id 订购;

Use This :

WITH CTE AS( select busId, count(*) as no_of_passenger from (select x.busId, x.psgid, Row_number() over( partition by x.psgId order by x.bustime) as rnk from (SELECT b.id as busId, p.id as psgId, p.time as pagtime, b.time as bustime FROM buses b JOIN passengers p ON p.origin = b.origin AND p.destination = b.destination and (p.time < b.time OR p.time=b.time))x )y where y.rnk=1
group by busId ) select b.id, ISNULL(c.no_of_passenger,0)
from CTE c Right Join buses b on b.id=c.busId order by b.id

选择 bb.Id, count(pb.id) 作为从巴士 bb 离开的计数加入乘客 pb on (bb.origen = pb.origen) and (bb.destino = pb.destino) and (pb.time <= bb.time ) 在不存在的情况下(选择 b.Id, b.time, b.origen, b.destino from bus b left 加入乘客 p on (b.origen = p.origen) and (b.destino = p.destino) and ( p.time <= b.time) 其中 (b.time < bb.time) and (b.origen = bb.origen) and (b.destino = bb.destino) and (p.id = pb.id) )按 bb.id 分组 按 bb.id 排序

选择 bus.id,coalesce(temp_3.no_passengers,0) from bus left join (select temp_2.id,count(*) as no_passengers from (select temp.id,temp.p_id,temp.origin,temp.destination,temp. bus_time,temp.p_time,(temp.bus_time - temp.p_time) 作为检查,min(temp.bus_time - temp.p_time) over (partition by temp.p_id) as fi from (select a.id,b.id as p_id ,a.origin,a.destination,((cast(substr(a.time,1,2) as int)*60) + cast(substr(a.time,4) as int)) as bus_time,((cast (substr(b.time,1,2) as int)*60) + cast(substr(b.time,4) as int)) 作为 p_time 从公共汽车 a 左加入乘客 b ((a.origin = b. origin) and (a.destination=b.destination) and (bus_time>=p_time))) temp) temp_2 where temp_2.checking = temp_2.fi group by temp_2.id) temp_3 on (buses.id=temp_3.id) order通过 bus.id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM