SQL: возврат из группы в таблице - PullRequest
0 голосов
/ 09 июля 2019

Я пытаюсь создать SQL-запрос в MS Access из таблицы, в которой есть профиль и много пользователей, и я могу определить, какие профили не имеют конкретного пользователя.

Пример Я хочу вернуть профили , которые не содержат пользователя UserA

Таблица профилей

Profile     User
A           UserA
A           UserB
A           UserC
A           UserD
B           UserB
B           UserC
C           UserA
D           UserV

выход

Profile
B
D

Ответы [ 3 ]

2 голосов
/ 09 июля 2019

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

select distinct profile from tablename as t
where not exists (
  select 1 from tablename as tt
  where tt.profile = t.profile and user = 'UserA'  
)
2 голосов
/ 09 июля 2019

Один метод - group by и having:

select p.profile
from profile as p
group by p.profile
having sum(iif(p.user = "UserA", 1, 0)) = 0;
1 голос
/ 09 июля 2019

Для чего это стоит, вы также можете использовать объединение:

select distinct t1.profile
from YourTable t1 left join 
(select t2.profile as p from YourTable t2 where t2.user = "UserA") t3 on t1.profile = t3.p
where t3.p is null

или not in:

select distinct t1.profile
from YourTable t1
where t1.profile not in (select t2.profile from YourTable t2 where t2.user = "UserA")

Измените YourTable на имя вашей таблицы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...