Iva*_*nko 7 sql oracle select group-by count
如果组中至少有一个值满足条件,如何创建组?
以下是 DB 表的示例test:
| ID | TYPE | COLOR |
|====|======|=======|
| 1 | 1 | R |
| 2 | 1 | B |
| 3 | 1 | G |
| 4 | 2 | B |
| 5 | 2 | G |
| 6 | 3 | G |
Run Code Online (Sandbox Code Playgroud)
我需要选择TYPE具有多于一行的所有值,并且该类型的至少一种颜色是 G。
所以伪选择看起来像这样:
select TYPE
from test
group by TYPE
having count(*) > 1
and count(COLOR = 'G') > 0
Run Code Online (Sandbox Code Playgroud)
随着 OP 的修改要求:
select type
from test
group by type
having count(*) > 1 and count(case when color = 'G' then 0 end) > 0
;
Run Code Online (Sandbox Code Playgroud)