具有特定值
问题描述:
我有一个表,看起来像这种两两行取得列:具有特定值
| col1 | col2 |
|------|------|
| a | 1 |
| a | 2 |
| a | 3 |
| b | 1 |
| b | 3 |
| c | 1 |
| c | 2 |
我需要找到col1
的值,其中存在两行具有相同col1
值具有COL2值1和2
结果将是:
| col1 |
|------|
| a |
| c |
答
您可以通过你想要的col2
值过滤行,然后组10,只需要该基团与count = 2
select col1
from yourTable
where col2 in (1, 2)
group by col1
having count(distinct col2) = 2
答
另一解决方案是
select col1
from your_table
group by col1
having sum(case when col2 = 1 then 1 else 0 end) > 0
and sum(case when col2 = 2 then 1 else 0 end) > 0