【问题标题】:DISTINCT ON (col) col_alias – alias not workingDISTINCT ON (col) col_alias – 别名不起作用
【发布时间】:2019-03-17 07:19:15
【问题描述】:

我正在尝试运行这个命令:

SELECT DISTINCT ON (_id) test FROM creator_map.infos;

这给了我错误:

错误:“测试”列不存在

但根据以下教程,test 应该是别名

SELECT
 DISTINCT ON
 (column_1) column_1_alias,
 column_2
FROM
 tbl_name
ORDER BY
 column_1,
 column_2;

Source.

如何让 distinct 成为别名?

【问题讨论】:

    标签: postgresql distinct-on


    【解决方案1】:

    您不能给on (...) 一个别名,因为它不是“返回”名称的表达式。这就像试图给 IN 条件一个别名 where a in (1,2,3) as foo

    如有疑问,请阅读the manual。它没有提到那里的别名。

    在实际选择列表中只能为表达式定义别名,(...) 不是选择列表的一部分,而是distinct on 表达式的一部分。

    我认为这只是该教程页面上措辞不当的问题,应该阅读:

    select distinct on (column1) 
              column1 as column_1_alias,
              column2
    from ....
    

    【讨论】:

      猜你喜欢
      • 2021-08-17
      • 1970-01-01
      • 2020-04-05
      • 2018-11-23
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      相关资源
      最近更新 更多