Reputation: 1167
I'm doing a quick query using Postgresql 8.2 and I've done queries like this a thousand times before, but I can't figure out why I'm getting this error. I probably am missing something obvious, but it's saying my "subquery in FROM must have an alias". I do have an alias for my subquery "inner", and I can't figure out why else I would be getting the error.
SELECT "Branch", "Zip_5", "CountofStops", avg("EarlyTime") As
"Average_Arrival_Time"
FROM
(SELECT branch_id as "Branch", substring(stop_zip_postal_code, 1, 5) as
"Zip_5", count(stop_name) as "CountofStops", min(actual_arrival_time) as
"EarlyTime"
FROM distribution_stop_information
WHERE company_no = '001' AND route_date > '3/13/2017'
GROUP BY branch_id, stop_zip_postal_code)
inner
GROUP BY "Branch", "Zip_5"
ORDER BY Zip_5
********** Error **********
ERROR: subquery in FROM must have an alias
SQL state: 42601
Hint: For example, FROM (SELECT ...) [AS] foo.
Upvotes: 1
Views: 6414
Reputation: 1270391
inner
. . . think "inner join". You need a better alias than that.
SELECT Branch, Zip_5, CountofStops, avg(EarlyTime) As Average_Arrival_Time
FROM (SELECT branch_id as Branch, left(stop_zip_postal_code, 5) as Zip_5,
count(stop_name) as CountofStops,
min(actual_arrival_time) as EarlyTime
FROM distribution_stop_information
WHERE company_no = '001' AND route_date > '2017-03-13'
GROUP BY branch_id, stop_zip_postal_code
) b
GROUP BY Branch, Zip_5
ORDER BY Zip_5;
Notes:
LEFT()
is a convenient shorthand for substring( . . ., 1, . . .)
Upvotes: 0