col1 col2 col3
100 FRANCE 201604
100 FRANCE 201604
100 AUSTRALIA 201605
200 MALAYSIA 201604
300 IRELAND 201604
300 IRELAND 201605
400 SPAIN 201604
500 FRANCE 201604
500 MEXICO 201604
我正在尝试根据col2和col3的条件从col1获取数据。我想从col1中消除所有与col2没有明显组合的值。在我的情况下,100和500将被淘汰。
现在我想看看每个月(col3)(201604和201605)是否有200,300,400,所以300只会出现。
我试过
select c1,count(*),c3 from
(
SELECT distinct col1 c1, col2 c2, col3
FROM table
) group by c1,c3 having count(*)=1;
答案 0 :(得分:1)
SELECT col1
FROM tablex t
GROUP BY col1
HAVING count(distinct col2) = 1
AND ( SELECT count(1)
FROM tablex t2
WHERE t2.col1 = t.col1
AND t2.col3 in (201604,201605)
) = 2
答案 1 :(得分:-1)
我想你想要:
select t.*
from t
where t.col1 in (select t2.col1
from (select t2.*, count(*) over (partition by col2, col3) as cnt
from t t2
) t2
group by t2.col1
having max(cnt) = 1
);
关键的想法是,max(t2.col1)
时会count(*) = 1
返回唯一值。
以下是example的工作原理。