如何计算 Table 交易中遗漏的零件以及 Table 零件上存在的零件?
How to count missed Parts from Table Trade and that Exist On Table Parts?
我在 sql 服务器 2012 上工作我遇到问题,我无法从交易代码 table 中获取遗漏的零件并且存在于 table 零件上。
首先我在 table 搜索数据上上传了 plid 和 codetypeid 的数据。
其次,我从基于 plid 的 table 部分中获取相关部分。
第三,我从 table 贸易代码中得到遗漏的部分。
意思是我需要让零件存在于 table 零件上并与 table 搜索数据相关,而不存在于 table 贸易代码
上
数据样本:
create table #searchdata
(
plid int,
codetypeid int
)
insert into #searchdata
(plid,codetypeid)
values
(84459,877490)
create table #parts
(
partid int,
plid int
)
insert into #parts(partid,plid)
values
(758901,84459),
(808091,84459),
(509030,84459),
(7090321,84459),
(32453,84459),
(45563,84459)
create table #tradecode
(
partid int,
codetypeid int
)
insert into #tradecode(partid,codetypeid)
values
(758901,877490),
(808091,877490)
select p.plid,s.codetypeid,count(p.partid) as countmissingParts
from #parts p
inner join #searchdata s on s.plid=p.plid
left join #tradecode t on t.codetypeid=s.codetypeid
where t.partid is null
group by p.plid,s.codetypeid
drop table #searchdata
drop table #parts
drop table #tradecode
我的尝试:
select p.plid,s.codetypeid,count(p.partid) as countmissingParts
from #parts p
inner join #searchdata s on s.plid=p.plid
left join #tradecode t on t.partid=s.plid
where t.partid is null
group by p.plid,s.codetypeid
预期结果
plid codetypeid countmissingParts
84459 877490 4
您非常接近您尝试过的查询...试一试。你的LEFT JOIN
,你加入了partid
到plid
...应该加入了p.partid=t.partid
select p.plid,s.codetypeid,count(p.partid) as countmissingParts
from #parts p
inner join #searchdata s on s.plid=p.plid
left join #tradecode t on p.partid=t.partid
where t.partid is null
group by p.plid,s.codetypeid
我在 sql 服务器 2012 上工作我遇到问题,我无法从交易代码 table 中获取遗漏的零件并且存在于 table 零件上。
首先我在 table 搜索数据上上传了 plid 和 codetypeid 的数据。
其次,我从基于 plid 的 table 部分中获取相关部分。
第三,我从 table 贸易代码中得到遗漏的部分。
意思是我需要让零件存在于 table 零件上并与 table 搜索数据相关,而不存在于 table 贸易代码
上数据样本:
create table #searchdata
(
plid int,
codetypeid int
)
insert into #searchdata
(plid,codetypeid)
values
(84459,877490)
create table #parts
(
partid int,
plid int
)
insert into #parts(partid,plid)
values
(758901,84459),
(808091,84459),
(509030,84459),
(7090321,84459),
(32453,84459),
(45563,84459)
create table #tradecode
(
partid int,
codetypeid int
)
insert into #tradecode(partid,codetypeid)
values
(758901,877490),
(808091,877490)
select p.plid,s.codetypeid,count(p.partid) as countmissingParts
from #parts p
inner join #searchdata s on s.plid=p.plid
left join #tradecode t on t.codetypeid=s.codetypeid
where t.partid is null
group by p.plid,s.codetypeid
drop table #searchdata
drop table #parts
drop table #tradecode
我的尝试:
select p.plid,s.codetypeid,count(p.partid) as countmissingParts
from #parts p
inner join #searchdata s on s.plid=p.plid
left join #tradecode t on t.partid=s.plid
where t.partid is null
group by p.plid,s.codetypeid
预期结果
plid codetypeid countmissingParts
84459 877490 4
您非常接近您尝试过的查询...试一试。你的LEFT JOIN
,你加入了partid
到plid
...应该加入了p.partid=t.partid
select p.plid,s.codetypeid,count(p.partid) as countmissingParts
from #parts p
inner join #searchdata s on s.plid=p.plid
left join #tradecode t on p.partid=t.partid
where t.partid is null
group by p.plid,s.codetypeid