2

I have 2 tables where primary key of first table is used as foreign key for second table. In first table primary key is having unique values but in second table as foreign key it is having duplicate values as well. Now i want count from both the tables at same time and using left join for that. I have tried the following query :-

 select distinct count(c.joined) as joined,
    count(t.JobReqID),t.Country from txrecruitment as t 
    left join CandidateDetails as c on t.JobReqID=c.Jobreqid 
    where t.status='open'
    group by t.Country

The count value of count(t.JobReqID) is not giving count of distinct JobReqId which i want. The result of count is counting the multiple instances of 2nd table also which i want to remove. What should be the right way to do this. Any help is welcomed!!

2

2 Answers 2

2

Count using DISTINCT for the JobReqID like below :

select count(c.joined) as joined, count(distinct t.JobReqID), t.Country 
from txrecruitment as t 
left join CandidateDetails as c on t.JobReqID=c.Jobreqid 
where t.status='open'
group by t.Country
Sign up to request clarification or add additional context in comments.

2 Comments

thanks both of you @kabir. It solved my problem but what can be the possible reason that count(c.joined) is giving count as one less than actual count. thanks again!!
It's great that your problem is sorted out. Don't forget to mark it as useful (upvote) :)
1

Because our distinct is not placed in the correct location. Use Distinct Inside the Count.

 select count(distinct  c.joined) as joined,
    count(DISTINCT t.JobReqID),t.Country from txrecruitment as t 
    left join CandidateDetails as c on t.JobReqID=c.Jobreqid 
    where t.status='open'
    group by t.Country

2 Comments

using count(distinct c.joined) is not helping, it is giving all the count as 1 @jayasurya
Might be because for each Country joined ID has only 1 unique value. In that cause , you may just remove the distinct from that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.