Postgresql“FROM中的子查询必须有别名”错误

时间:2017-05-15 19:57:18

标签: sql postgresql

我正在使用Postgresql 8.2进行快速查询,之前我已经完成了一千次这样的查询,但我无法弄清楚为什么我会收到此错误。我可能错过了一些明显的东西,但它说我的“FROM中的子查询必须有一个别名”。我的子查询“内部”有一个别名,我无法弄清楚为什么我会收到错误。

    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.

2 个答案:

答案 0 :(得分:2)

inner是保留关键字。使用其他名称作为别名。

答案 1 :(得分:0)

inner。 。 。想想“内心的联系”。你需要一个更好的别名。

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;

注意:

  • 除非需要,否则不要将列名称用双引号括起来。它们只是多余的。
  • 使用日期常量的标准格式。
  • LEFT()substring( . . ., 1, . . .)
  • 的便捷简写