我有表A,表B和表C.
我想从表A中选择2个字段,从表B中选择一个字段(我假设这两个字段之间是内连接),并在表C中找到表B上标识符的每个记录的计数。
所以我会:
OpeartiveId | OperativeNumber | JobLocation | CountOfJobIdInWorkTable
编辑:
Operative
OperativeId
OperativeNumber
Jobs
JobId
JobLocation
Work
JobId
OperativeId
答案 0 :(得分:2)
假设您还希望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;
答案 1 :(得分:1)
将其翻译成您的实际架构并尝试:
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
答案 2 :(得分:1)
不确定这是否是您要找的。看着你提供的文件,我想出了这个。
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
答案 3 :(得分:-1)
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