Select all columns from rows distinct on one column

只谈情不闲聊 提交于 2019-12-12 03:39:02

问题


I am using Netezza (based on PostgreSQL) and need to select all columns in a table for rows distinct on one column. A related question with answer can be found here, but it doesn't handle the case with all columns, going by that answer throws an error:

select distinct on (some_field) table1.* from table1 order by some_field;

Snippet from error with real data:

"(" (at char 77) expecting '')''


回答1:


I don't think your code should throw an error in Postgres. However, it won't do what you expect without an order by:

select distinct on (some_field) table1.*
from table1
order by some_field;



回答2:


The syntax of your query is correct for Postgres (like you declared at first). See:

  • Select first row in each GROUP BY group?

You later clarified you actually work with Netezza, which is only loosely related to Postgres. Wikipedia states:

Netezza is based on PostgreSQL 7.2,[8] but does not maintain compatibility.

Netezza does not seem to support DISTINCT ON (), only DISTINCT.

It supports row_number(), though. So this should work:

SELECT *
FROM  (
   SELECT *, row_number() OVER (PARTITION BY some_field) AS rn
   FROM   table1
   ) sub
WHERE  rn = 1;

The question remains: Which row do you want from each set with identical some_field. If any row is good, you are done here. Else, you need to add ORDER BY to the OVER clause.

Related:

  • SQL to get unique rows in Netezza DB


来源:https://stackoverflow.com/questions/41894413/select-all-columns-from-rows-distinct-on-one-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!