How to get missing parts on trade code table and that exist on table #parts and #search data?

ahmed salah 3,216 Reputation points
2020-08-31T01:39:54.81+00:00

I work on SQL server 2012 I need to count missing rows Part id that exist on table #parts
and not exist on table #tradecode

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

Expected result is :slight_smile:

plid codetypeid countmissingParts
84459 877490 4

actually I put data for search and filter on table search data

I need to get data exist on search data table and that exist on parts table

but not exist on table trade code

so it must have 4 parts or rows missing but statement above

return null rows

why and how to solve

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,657 questions
{count} votes

Accepted answer
  1. Anonymous
    2020-09-01T08:28:39.93+00:00

    If you are learning SQL then you can experiment with other solutions as well. For example:

    select p.plid, s.codetypeid, count(p.partid) as countmissingParts
    from parts p
    inner join (select partid from parts except select partid from tradecode) f on f.partid = p.partid
    inner join searchdata s on s.plid=p.plid
    group by p.plid,s.codetypeid
    
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Tom Cooper 8,471 Reputation points
    2020-08-31T03:14:18.117+00:00
    ;With cte As
    (Select s.plid, Count(*) As DesiredCount
    From #searchdata s
    Inner Join #parts p On p.plid = s.plid
    Group By s.plid)
    
    Select s.plid, c.DesiredCount - Count(*) As CountMisssingParts
    From #searchdata s
    Inner Join cte c On c.plid = s.plid
    Left Join #tradecode t On t.codetypeid = s.codetypeid
    Left Join #parts p On p.partid = t.partid
    Group By s.plid, s.codetypeid, c.DesiredCount
    Having c.DesiredCount - Count(*) > 0;
    

    Tom

    0 comments No comments

  2. ahmed salah 3,216 Reputation points
    2020-09-01T06:13:52.573+00:00

    thank you for reply
    code above give me correct result but depend on count different
    are there are any way give me missing count without subtract
    meaning count missing depend on data it self

    0 comments No comments

  3. Tom Cooper 8,471 Reputation points
    2020-09-01T06:58:14.947+00:00
     Select s.plid, s.codetypeid, Count(*) As CountMisssingParts
     From #searchdata s
     Inner Join #parts p On p.plid = s.plid
     Left Join #tradecode t On t.partid = p.partid
     Where t.partid Is Null
     Group By s.plid, s.codetypeid;
    

    Tom

    0 comments No comments

  4. Anonymous
    2020-09-01T07:31:03.12+00:00

    Following your thinking, your original query should have written it like this:

    select p.plid, s.codetypeid, count(p.partid) as countmissingParts
    from #parts p
    inner join #searchdata s on s.plid=p.plid
    where not exists(select 1 from #tradecode t where t.partid = p.partid)
    group by p.plid,s.codetypeid
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.