Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i want to get the records from the table which are not available in actual result of a table. the scenario is as follows.

My Actual Result

query which i have written:-

SQL
select (cont.FirstName+' '+ISNULL(cont.LastName,'')) as withoutduplicationName
from Contacts cont left join Cities ct on cont.Location=ct.CityId join States st
on cont.WorkState=st.StateId join Recruiters rec on cont.Recruiter=rec.RecruiterId join Team t on
rec.Team=t.TeamId where cont.IsDeleted='false' and rec.IsDeleted='false'


o/p:- it is displaying 534 (count) records.

Eliminating duplicates.

query is as follows
SQL
select distinct (cont.FirstName+' '+ISNULL(cont.LastName,'')) as withoutduplicationName
from Contacts cont left join Cities ct on cont.Location=ct.CityId join States st
on cont.WorkState=st.StateId join Recruiters rec on cont.Recruiter=rec.RecruiterId join Team t on
rec.Team=t.TeamId where cont.IsDeleted='false' and rec.IsDeleted='false'


o/P:- it is displaying 495 records.

Now i want to get the records which are not available in my actual result.

i.e 534-495=39 records?

so how to get thest 39 records as an o/p?
please help me to solve this, i have used except condition in sql but it is not giving proper o/p.
Posted

1 solution

try this
SQL
select  
   (cont.FirstName+' '+ISNULL(cont.LastName,'')) as duplicationName,
    count(1) as NoOfRecords
from Contacts cont 
left join Cities ct on cont.Location=ct.CityId 
join States st on cont.WorkState=st.StateId 
join Recruiters rec on cont.Recruiter=rec.RecruiterId 
join Team t on rec.Team=t.TeamId 
where cont.IsDeleted='false' and rec.IsDeleted='false'
group by (cont.FirstName+' '+ISNULL(cont.LastName,''))
having count(1)>1
 
Share this answer
 
v3
Comments
pavan_ kumar 27-Nov-14 3:04am    
it is not giving proper result, it is showing 32 records instead of 39.
Shweta N Mishra 27-Nov-14 3:08am    
Its correct, But it means that there are duplicate Name records More than 2

you can check by above query again, i am modifying it.
pavan_ kumar 27-Nov-14 3:25am    
yes,you are right, thanks a lot
Shweta N Mishra 27-Nov-14 3:31am    
np :) Mark the answer as accepted, if it worked for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900