如果我有这样的桌子。
col1 col2 col3
1 2 3
1 3 2
1 2 1
1 2 2
1 2 3
我只想要col3中所有三个值(1,2,3)的col2值
col2
2
如何获得像这样的人的结果?
答案 0 :(得分:3)
select col2
from your_table
group by col2
having count(distinct col3) = (select count(distinct col3) from your_table)
答案 1 :(得分:0)
假设1,2和3是col3唯一可接受的值(并且该列不为null),那么你可以这样做:
with sample_data as (select 1 col1, 2 col2, 3 col3 from dual union all
select 1 col1, 3 col2, 2 col3 from dual union all
select 1 col1, 2 col2, 1 col3 from dual union all
select 1 col1, 2 col2, 2 col3 from dual union all
select 1 col1, 2 col2, 3 col3 from dual),
res as (select col1,
col2,
col3,
count(distinct col3) over (partition by col1, col2) cnt_distinct_col3
from sample_data)
select col1,
col2,
col3
from res
where cnt_distinct_col3 = 3;
COL1 COL2 COL3
---------- ---------- ----------
1 2 1
1 2 2
1 2 3
1 2 3
我不确定你的sample_data是否只是从col2开始,或者是col1 + col2;例如,如果样本数据是:
COL1 COL2 COL3
---------- ---------- ----------
1 2 3
1 3 2
1 2 1
1 2 2
1 2 3
2 3 1
2 3 3
您是否希望看到col2 = 2和col2 = 3行返回,或者您只希望看到(col1,col2)=(1,2)行返回?
我假设后者,但如果它是前者,那只是从分析函数中的分区子句中取出col1的情况。然后,当然,将选择更改为“选择不同的col2”。
因此,根据您希望如何显示答案,我认为我的查询适用于您希望在结果中显示col1(即未过滤)以及何时希望按特定col1值过滤的情况,例如;
with sample_data as (select 1 col1, 2 col2, 3 col3 from dual union all
select 1 col1, 3 col2, 2 col3 from dual union all
select 1 col1, 2 col2, 1 col3 from dual union all
select 1 col1, 2 col2, 2 col3 from dual union all
select 1 col1, 2 col2, 3 col3 from dual union all
select 2 col1, 3 col2, 1 col3 from dual union all
select 2 col1, 3 col2, 2 col3 from dual union all
select 2 col1, 3 col2, 3 col3 from dual union all
select 2 col1, 2 col2, 1 col3 from dual union all
select 3 col1, 1 col2, 2 col3 from dual union all
select 3 col1, 1 col2, 3 col3 from dual),
res as (select col1,
col2,
col3,
count(distinct col3) over (partition by col1, col2) cnt_distinct_col3
from sample_data)
select distinct col1,
col2
from res
where cnt_distinct_col3 = 3;
COL1 COL2
---------- ----------
2 3
1 2
with sample_data as (select 1 col1, 2 col2, 3 col3 from dual union all
select 1 col1, 3 col2, 2 col3 from dual union all
select 1 col1, 2 col2, 1 col3 from dual union all
select 1 col1, 2 col2, 2 col3 from dual union all
select 1 col1, 2 col2, 3 col3 from dual union all
select 2 col1, 3 col2, 1 col3 from dual union all
select 2 col1, 3 col2, 2 col3 from dual union all
select 2 col1, 3 col2, 3 col3 from dual union all
select 2 col1, 2 col2, 1 col3 from dual union all
select 3 col1, 1 col2, 2 col3 from dual union all
select 3 col1, 1 col2, 3 col3 from dual),
res as (select col1,
col2,
col3,
count(distinct col3) over (partition by col1, col2) cnt_distinct_col3
from sample_data)
select distinct col2
from res
where cnt_distinct_col3 = 3
and col1 = 1;
COL2
----------
2
好的,我认为这就是你所追求的:
with sample_data as (select 1 col1, 2 col2, 3 col3 from dual union all
select 1 col1, 3 col2, 2 col3 from dual union all
select 1 col1, 2 col2, 1 col3 from dual union all
select 1 col1, 2 col2, 2 col3 from dual union all
select 1 col1, 2 col2, 3 col3 from dual union all
select 2 col1, 3 col2, 1 col3 from dual union all
select 2 col1, 3 col2, 2 col3 from dual union all
select 2 col1, 3 col2, 3 col3 from dual union all
select 2 col1, 2 col2, 1 col3 from dual union all
select 3 col1, 1 col2, 2 col3 from dual union all
select 3 col1, 1 col2, 3 col3 from dual),
res as (select col1,
col2,
col3,
count(distinct col3) over (partition by col1, col2) cnt_distinct_col3_per_col1col2,
count(distinct col3) over (partition by col1) cnt_distinct_col3_per_col1
from sample_data)
select distinct col2
from res
where cnt_distinct_col3_per_col1col2 = cnt_distinct_col3_per_col1
and col1 = 1;
COL2
----------
2
这种方式的工作原理是: