我有一个如下数据库
Column1 column2 column3
A123 abc Def
A123 xyz Abc
B456 Gh Ui
我想通过sql命令选择第1列中没有相同内容的行。
在这种情况下,预期结果仅在第3行。 怎么做?
谢谢
答案 0 :(得分:1)
您可以将带有带有subselect的联接用于计数= 1
select * from my_table m
inner join (
select column1, count(*)
from my_table
group by column_1
having count(*) =1
) t on t.column_1 = m.column_1
答案 1 :(得分:0)
WITH CTE AS (Select COUNT(Column1) OVER(PARTITION BY Column1 ) as coincident,* from table )Select * from CTE where coincident =1
答案 2 :(得分:0)
我将使用窗口功能:
select Column1, column2, column3
from (select t.*, count(*) over (partition by column1) as cnt
from t
) t
where cnt = 1;
但是,还有其他有趣的方式。例如,聚合:
select column1, max(column2) as column2, max(column3) as column3
from t
group by column1
having count(*) = 1;
或者,如果您知道其他列之一在不同的行上具有不同的值,那么not exists
可能是最有效的解决方案:
select t.*
from t
where not exists (select 1
from t t2
where t2.column1 = t.column1 and
t2.column2 <> t.column2
);