Estou lutando com consultas realmente complexas envolvendo duas subconsultas usando WITH
cláusula conforme descrito aqui .
Mas parece-me que não posso usar uma subconsulta dentro da minha consulta principal. Eu aqui está minha declaração de consulta:
WITH trip_labelled AS
(
SELECT DISTINCT ON (trips_1.trip_id) trips_1.trip_id,
sum(segments.length::double precision) AS segments_length,
sum(travelmode_profile.foot * segments.length)::real / GREATEST(sum(segments.length), 1::real) AS foot,
sum(travelmode_profile.bike * segments.length)::real / GREATEST(sum(segments.length), 1::real) AS bike,
sum(travelmode_profile.car * segments.length)::real / GREATEST(sum(segments.length), 1::real) AS car,
sum(travelmode_profile.bus * segments.length)::real / GREATEST(sum(segments.length), 1::real) AS bus,
sum(travelmode_profile.metro * segments.length)::real / GREATEST(sum(segments.length), 1::real) AS metro
FROM trips trips_1
JOIN segments ON segments.session_id = ANY (trips_1.session_ids)
JOIN travelmode_profile USING (session_id, segment_id)
LEFT JOIN session USING (session_id)
LEFT JOIN ( SELECT DISTINCT ON (session_1.user_id) session_1.user_id,
response.response AS role
FROM session session_1
LEFT JOIN response USING (session_id)
WHERE response.question_id = 24) demography USING (user_id)
WHERE segments.movement = true
GROUP BY trips_1.trip_id
),
trip_mode AS
( SELECT trips.trip_id AS trip_id,
trips.seconds_start,
trips.seconds_end,
trips.distance,
CASE
WHEN GREATEST(trip_labelled.car, trip_labelled.bike, trip_labelled.bus, trip_labelled.metro, trip_labelled.foot) = trip_labelled.car THEN 'car'::text
WHEN GREATEST(trip_labelled.car, trip_labelled.bike, trip_labelled.bus, trip_labelled.metro, trip_labelled.foot) = trip_labelled.bike THEN 'bike'::text
WHEN GREATEST(trip_labelled.car, trip_labelled.bike, trip_labelled.bus, trip_labelled.metro, trip_labelled.foot) = trip_labelled.bus THEN 'bus'::text
WHEN GREATEST(trip_labelled.car, trip_labelled.bike, trip_labelled.bus, trip_labelled.metro, trip_labelled.foot) = trip_labelled.metro THEN 'metro'::text
WHEN GREATEST(trip_labelled.car, trip_labelled.bike, trip_labelled.bus, trip_labelled.metro, trip_labelled.foot) = trip_labelled.foot THEN 'foot'::text
ELSE NULL::text
END AS travel_mode
FROM trips
JOIN trip_labelled USING (trip_id)
)
SELECT session_id,
COALESCE(gps.gpstime, gslocation.gpstime) AS gpstime,
COALESCE(gps.lat, gslocation.lat) AS lat,
COALESCE(gps.lon, gslocation.lon) AS lon,
COALESCE(gps.alt, gslocation.alt) AS alt,
COALESCE(gps.speed, gslocation.speed) AS speed,
COALESCE(gps.acc, gslocation.acc) AS acc
trip_mode.travel_mode
FROM raw_data.gslocation
FULL JOIN raw_data.gps
USING (session_id, seconds, millis);
Erro:
ERROR: syntax error at or near "trip_mode"
LINE 53: trip_mode.travel_mode
Não consigo usar travel_mode
ou trip_mode.travel_mode
da segunda subconsulta trip_mode
na consulta principal. Mas funciona comentando travel_mode/trip_mode.travel_mode
. Onde eu erro?
Há dois problemas com sua consulta:
Está faltando uma vírgula antes
trip_mode.travel_mode
.Se você quiser fazer referência aos CTEs na consulta principal, será necessário adicioná-los à
FROM
cláusula.Você precisa usar parênteses em torno da instrução case.
Tente isto: