选择在给定范围内具有子标的容器

发布于 2025-02-11 01:41:59 字数 1396 浏览 2 评论 0原文

我有两张表:

项目:

ID名称
1ABC
2DEF
3GHI
4JKL
5MNO
6PQR

附件:

ID状态ProjectID
1A11
A112
3A2A1 1
A2 2 4A22
43 6 A23
7A2 2 5 A1 3 6 A2A1
4A14
8 A2 4A24
9A34
10A15
11A25
12A45

我想获得的项目名称,该项目名称只有具有状态A1和A2的作业(必须两个),并且不会在状态A3和A4中进行分配。 因此,结果应该是:

GHI

我尝试的是:

select distinct 
p.Name 
from 
Attachment a 
inner join Project p on p.Id = a.ProjectId 
group by 
p.Name, a.status 
having (a.Status = 'a1' or a.Status = 'a2'

I have two tables:

Project:

IdName
1ABC
2DEF
3GHI
4JKL
5MNO
6PQR

Attachment:

IdStatusProjectId
1a11
2a11
3a22
4a22
5a13
6a23
7a14
8a24
9a34
10a15
11a25
12a45

I'd like to get projectnames which has assignments only with statuses a1 and a2 (must have both of them) and doesn't havve assignments in statuses a3 and a4.
So the result should be:

GHI

What I've tried wass:

select distinct 
p.Name 
from 
Attachment a 
inner join Project p on p.Id = a.ProjectId 
group by 
p.Name, a.status 
having (a.Status = 'a1' or a.Status = 'a2'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傻比既视感 2025-02-18 01:41:59

您可以使用存在|不存在关键字来实现这一目标。

select [Name] from Project t1
where 
    exists (select 1 from Attachment where [Status] = 'a1' and ProjectId = t1.Id)
and
    exists (select 1 from Attachment where [Status] = 'a2' and ProjectId = t1.Id)

and 
    not exists (select 1 from Attachment where [Status] = 'a3' and ProjectId = t1.Id)
and
    not exists (select 1 from Attachment where [Status] = 'a4' and ProjectId = t1.Id)

You can use exist|not exists keywords to achieve this.

select [Name] from Project t1
where 
    exists (select 1 from Attachment where [Status] = 'a1' and ProjectId = t1.Id)
and
    exists (select 1 from Attachment where [Status] = 'a2' and ProjectId = t1.Id)

and 
    not exists (select 1 from Attachment where [Status] = 'a3' and ProjectId = t1.Id)
and
    not exists (select 1 from Attachment where [Status] = 'a4' and ProjectId = t1.Id)
暗恋未遂 2025-02-18 01:41:59

使用String_aggsubqueryjoin以获取所需的结果

SELECT name
FROM   (SELECT P.name,
               String_agg(status, ',')
                 within GROUP (ORDER BY status) Status
        FROM   project P
               join attachment A
                 ON A.projectid = P.id
        GROUP  BY P.name) t
WHERE  status = 'a1,a2'  

dbfiddle

use String_agg,Subquery and join to get your desired result

SELECT name
FROM   (SELECT P.name,
               String_agg(status, ',')
                 within GROUP (ORDER BY status) Status
        FROM   project P
               join attachment A
                 ON A.projectid = P.id
        GROUP  BY P.name) t
WHERE  status = 'a1,a2'  

dbfiddle

风渺 2025-02-18 01:41:59

您可以将计数与非在:

select P.name
from Project P
left join Attachment A on P.id = A.ProjectId
where p.id not in (select A2.ProjectId 
                   from Attachment A2 
                   where A2.status in ('a3', 'a4'))
and A.status in ('a1', 'a2')
group by P.name
having count(distinct A.status) = 2

You can use count distinct in combination with NOT IN:

select P.name
from Project P
left join Attachment A on P.id = A.ProjectId
where p.id not in (select A2.ProjectId 
                   from Attachment A2 
                   where A2.status in ('a3', 'a4'))
and A.status in ('a1', 'a2')
group by P.name
having count(distinct A.status) = 2

Here is a demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文