简体   繁体   English

同时在多个列上选择不重复,并在PostgreSQL中保留一列

[英]Select distinct on multiple columns simultaneously, and keep one column in PostgreSQL

For a table such as this: 对于这样的表:

tblA  

A,B,C  
1,2,t3a  
1,3,d4g  
1,2,b5e  
1,3,s6u 

I want to produce a table that selects distinct on both A and B simultaneously, and still keep one value of C, like so: 我想生成一个同时在A和B上选择不同的表,并且仍然保留一个C值,如下所示:

tblB  

A,B,C  
1,2,t3a  
1,3,d4g 

Seems like this would be simple, but not finding it for the life of me. 看起来这很简单,但在我的一生中找不到它。

DROP TABLE IF EXISTS tblA CASCADE;  
SELECT DISTINCT ON (A,B), C  
INTO tblB  
FROM tblA;  

When you use DISTINCT ON you should have ORDER BY : 当您使用DISTINCT ON您应该具有ORDER BY

SELECT DISTINCT ON (A,B), C
INTO tblB
FROM tblA
ORDER BY A, B;

This should do the trick 这应该可以解决问题

CREATE TABLE tblB AS (
    SELECT A, B, max(C) AS max_of_C FROM tblA GROUP BY A, B
)

Use a view to do the distinct and then join it to the original table to pick one row of column C. Inserting into the target is left for you to figure out. 使用视图进行区分,然后将其连接到原始表以选择C列的一行。将插入目标中的操作留给您找出。 Oh, and you could pick up multiple columns from t, not just c - the only thing is that your subquery needs to find a way to limit it to only one row. 哦,您可以从t中获取多个列,而不仅仅是c-唯一的事情是您的子查询需要找到一种将其限制为仅一行的方法。

create table t (a int, b int, c int);

create view tv as select distinct a, b from t;

insert into t (a, b, c) values(1, 2, 10);
insert into t (a, b, c) values(1, 2, 20);
insert into t (a, b, c) values(1, 3, 30);
insert into t (a, b, c) values(1, 3, 40);

CREATE TABLE tblB AS (
select tv.a, tv.b, t.c  from tv, t
where tv.a = t.a and tv.b = t.b
/* pick smallest ctid which is a unique row id built into postgres */
and t.ctid = (select min(ctid) from t s where s.a = t.a and s.b = t.b);
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM