基本连接和聚合?

发布于 2025-01-01 07:54:56 字数 342 浏览 2 评论 0原文

我有表 A、表 B 和表 C。

我想从表 A 中选择 2 个字段,从表 B 中选择一个字段(我假设这两个字段之间存在内部联接)以及表 B 上的标识符所在的每条记录的计数在表 C 上找到。

所以我将:

OpeartiveId | OperativeNumber | JobLocation | CountOfJobIdInWorkTable

编辑:

Operative
OperativeId
OperativeNumber

Jobs
JobId
JobLocation

Work
JobId
OperativeId

I have table A, table B and table C.

I want to select 2 fields from table A, one field from table B (an inner join between these two I'm assuming) and the Count for each record where the identifier on table B is found on table C.

So I'll have:

OpeartiveId | OperativeNumber | JobLocation | CountOfJobIdInWorkTable

Edit:

Operative
OperativeId
OperativeNumber

Jobs
JobId
JobLocation

Work
JobId
OperativeId

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

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

发布评论

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

评论(4

始终不够爱げ你 2025-01-08 07:54:56

假设您还想要 a 和 b 中的记录在 c 中没有匹配的记录,则需要一个外连接:

select a.pk_a, b.pk_b, count(c.pk_c) 
from a 
inner join b on a.pk_a = b.pk_a 
left outer join c on b.pk_b = c.pk_b 
group by a.pk_a, b.pk_b;

Assuming you also want records from a and b that have no matching records in c, you'll need an outer join:

select a.pk_a, b.pk_b, count(c.pk_c) 
from a 
inner join b on a.pk_a = b.pk_a 
left outer join c on b.pk_b = c.pk_b 
group by a.pk_a, b.pk_b;
梦里人 2025-01-08 07:54:56

将其转换为您的实际架构并尝试:

select a.one, a.two, b.three, count(c.id)
from a
join b on a.id=b.id
left join c on c.bid=b.id
group by a.one, a.two, b.three, b.id

Translate this to your actual schema and try it:

select a.one, a.two, b.three, count(c.id)
from a
join b on a.id=b.id
left join c on c.bid=b.id
group by a.one, a.two, b.three, b.id
小红帽 2025-01-08 07:54:56

不确定这是否是您要找的。看着你提供的文件,我想出了这个。

select o.operativeId, o.OperativeNumber, j.JobLocation, COUNT(w.jobId)
from dbo.Operative o
Inner join works w
    ON o.OperativeID = w.OperativeId
INNER JOIN jobs j
ON w.jobId = j.jobId
GROUP by o.operativeId, o.OperativeNumber, j.JobLocation

Not sure if this is what you looking for. looking at the filed you provided i came up with this.

select o.operativeId, o.OperativeNumber, j.JobLocation, COUNT(w.jobId)
from dbo.Operative o
Inner join works w
    ON o.OperativeID = w.OperativeId
INNER JOIN jobs j
ON w.jobId = j.jobId
GROUP by o.operativeId, o.OperativeNumber, j.JobLocation
痴意少年 2025-01-08 07:54:56
select a.a,a.b,b.a,(select count(*) from c where c.b_id = b.id)
from a 
join b on a.b_id = b.id
select a.a,a.b,b.a,(select count(*) from c where c.b_id = b.id)
from a 
join b on a.b_id = b.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文