获取只有在postresql

问题描述:

独特的价值观,我需要这样的:获取只有在postresql

SELECT * FROM TABLE WHERE <value in column1 is always unique 
(if ever any value will be noticed more than once, then skip this row)> 
PostgreSQL中

所以,如果我在表中的这些行:

1;"something";"xoxox" 
2;"other";"xoxox" 
3;"something";"blablabla" 

然后与查询去的话,那应该是结果:

2;"other";"xoxox" 

任何想法?

+0

你的描述说,“列1”独特的,但您的输出似乎用列2 –

+0

首先是ID,不计为列:P – Seric

使用count(*)作为窗口函数:

select t.* 
from (select t.*, count(*) over (partition by col1) as cnt 
     from t 
    ) t 
where cnt = 1; 

或者,你可以使用not existsid列:

select t.* 
from t 
where not exists (select 1 from t t2 where t2.col1 = t.col1 and t2.id <> t.id); 
+0

谢谢,它的工作! (我用第二个):) – Seric

您可以通过count过滤,而不需要一个子查询:

SELECT t.col1 
FROM t 
GROUP BY col1 
HAVING COUNT(*) = 1 

其他列可以通过使用聚合功能被添加像max,因为只会有每值1行:

SELECT t.col1, max(t.col2), max(t.col3) 
FROM t 
GROUP BY col1 
HAVING COUNT(*) = 1