Фильтр SQL по типу по вторичному идентификатору - PullRequest
0 голосов
/ 18 декабря 2018

У меня есть эта таблица:

id idsec type
1  a     color
2  a     description
3  b     description
4  b     food
5  b     animal
6  c     color
7  d     descritpion

Я бы хотел выбрать все idsec, которые не имеют тип "color", сгруппированные по idsec.

Ответы [ 4 ]

0 голосов
/ 18 декабря 2018

Вам нужны различные значения idsec из таблицы:

SELECT 
  DISTINCT idsec 
FROM tablename t 
WHERE
  NOT EXISTS (
    SELECT 1 FROM tablename WHERE tablename.idsec = t.idsec AND tablename.type = 'color'
  );
0 голосов
/ 18 декабря 2018

Используйте код ниже: -

select idsec from table 
where idsec not in (select idsec from table where type = 'color')
group by idsec
0 голосов
/ 18 декабря 2018

Один вариант будет использовать minus оператор набора

with t(id,idse,type) as
(
 select 1,'a','color' from dual union all
 select 2,'a','description' from dual union all
 select 3,'b','description' from dual union all
 select 4,'b','food' from dual union all
 select 5,'b','animal' from dual union all
 select 6,'c','color' from dual union all
 select 7,'d','descritpion' from dual
)
select idse from t group by idse
minus   
select idse from t where type = 'color';

IDSE
----
 b
 d

или

select distinct idse from t
minus   
select idse from t where type = 'color';

IDSE
----
 b
 d
0 голосов
/ 18 декабря 2018

Вы можете использовать not exists:

select t.*
from table t
where not exists (select 1 from table t1 where t1.idsec = t.idsec and t1.type = 'color');

Вы также можете сделать агрегацию:

select idsec
from table t
group by idsec
having sum(case when type = 'color' then 1 else 0 end) = 0;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...