2016-12-15 134 views
2

這是我的第一選擇查詢連接兩個選擇查詢PostgreSQL中

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兩次而不是加入的時間。 任何幫助表示讚賞。

回答

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 ; 
+0

現在,我得到我在那裏wrong.thanks很多 – RKR

+0

歡迎你的外部查詢,高興有幫助 – e4c5