1

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.
0

2 Answers 2

2

inner is a reserved keyword. Use another name as alias.

Sign up to request clarification or add additional context in comments.

1 Comment

D'oh! Easy enough of a solution! Why didn't I think of that? Thanks.
0

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:

  • Don't wrap column names in double quotes unless needed. They are just superfluous.
  • Use standard formats for date constants.
  • LEFT() is a convenient shorthand for substring( . . ., 1, . . .)

1 Comment

@a_horse_with_no_name . . . "Branch" is defined in the subquery. If the double quotes are removed there they are not needed in the outer query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.