在PostgreSQL中,使用别名表选择不同的列无法正常工作。

4
 SELECT pl_id,
    distinct ON (store.store_ID),
    in_user_id
    FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store 
    ON copy1.PLAN_ID = store .PLAN_ID;

在Postgres服务器上运行此查询时,我收到以下错误..如何使用distinct子句..在上面的代码计划1是模式名称。
错误:语法错误,在“distinct”附近的第2行: distinct ON (store.store_ID),

“distinct on ()” 需要放在 “select” 列表的开头。 - user330315
1个回答

2
您缺少一个 order by,其中第一组行应该是在 distinct on 子句中指定的那些行。此外,distinct on 子句应该位于选择列表的开头。
请尝试以下操作:
SELECT distinct ON (store_ID) store.store_ID, pl_id,
    in_user_id
    FROM plan1.plan_copy_levl copy1
INNER JOIN plan1._PLAN_STORE store 
    ON copy1.PLAN_ID = store .PLAN_ID
    order by store_ID, pl_id;

感谢..现在它抛出了错误:ERROR:","附近的语法错误,行号275:distinct ON(store.store_ID), - vignesh ramanathan
@vigneshramanathan - 已更新。请尝试使用更新后的答案? - Gurwinder Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接