2017-04-14 87 views
0

我有一个表,看起来像这种两两行取得列:具有特定值

| col1 | col2 | 
|------|------| 
| a | 1 | 
| a | 2 | 
| a | 3 | 
| b | 1 | 
| b | 3 | 
| c | 1 | 
| c | 2 | 

我需要找到col1的值,其中存在两行具有相同col1值具有COL2值1和2

结果将是:

| col1 | 
|------| 
| a | 
| c | 

回答

4

您可以通过你想要的col2值过滤行,然后组10,只需要该基团与count = 2

select col1 
from yourTable 
where col2 in (1, 2) 
group by col1 
having count(distinct col2) = 2 
0

另一解决方案是

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