这是我的第一个选择查询
SELECT date_trunc('hour',start_time) as time , extract(day from start_time) as day ,start_zone_id as zone_id, count(*) as booking_trips
FROM public.trip_information_september where trip_type='{BOOKING}' group by date_trunc('hour',start_time ), start_zone_id ,extract(day from start_time) ORDER BY time,start_zone_id
这是我的第二个选择查询
SELECT date_trunc('hour',start_time) as time , extract(day from start_time) as day ,start_zone_id as zone_id, count(*) as booking_trips
FROM public.trip_information_september where trip_type='{NO_INFO}' group by date_trunc('hour',start_time ), start_zone_id ,extract(day from start_time) ORDER BY time,start_zone_id
所以当我加入这两个
时select * from
(SELECT date_trunc('hour',start_time) as time , extract(day from start_time) as day ,start_zone_id as zone_id, count(*) as booking_trips
FROM public.trip_information_september where trip_type='{BOOKING}' group by date_trunc('hour',start_time ), start_zone_id ,extract(day from start_time) ORDER BY time,start_zone_id )A
INNER JOIN
(SELECT date_trunc('hour',start_time ) as time,extract(day from start_time) as day, start_zone_id as zone_id, count(*) as normal_trips
FROM public.trip_information_september where trip_type='{NORMAL}' group by date_trunc('hour',start_time ), start_zone_id ,extract(day from start_time) ORDER BY time,start_zone_id) B
ON A.time=B.time and A.zone_id=B.zone_id and A.day=B.day limit 100 ;
我得到时间,天,zone_id两次而不是加入的。 任何帮助表示赞赏。
答案 0 :(得分:1)
这并不奇怪,因为你的子查询连接包含在一个外部查询中,它会执行SELECT *你想要的东西
SELECT A.time, A.day, A.zone_id, booking_trips /*, ... REST OF THE REQUIRED COLUMNS */ FROM
(SELECT date_trunc('hour',start_time) as time , extract(day from start_time) as day ,start_zone_id as zone_id, count(*) as booking_trips
FROM public.trip_information_september where trip_type='{BOOKING}' group by date_trunc('hour',start_time ), start_zone_id ,extract(day from start_time) ORDER BY time,start_zone_id )A
INNER JOIN
(SELECT date_trunc('hour',start_time ) as time,extract(day from start_time) as day, start_zone_id as zone_id, count(*) as normal_trips
FROM public.trip_information_september where trip_type='{NORMAL}' group by date_trunc('hour',start_time ), start_zone_id ,extract(day from start_time) ORDER BY time,start_zone_id) B
ON A.time=B.time and A.zone_id=B.zone_id and A.day=B.day limit 100 ;