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!!

  • Please share your expected result and actual result as text. – TheGameiswar Nov 13 '17 at 07:22
  • Possible duplicate of [SQL Server query - Selecting COUNT(\*) with DISTINCT](https://stackoverflow.com/questions/1521605/sql-server-query-selecting-count-with-distinct) – kvk30 Nov 13 '17 at 07:24

2 Answers2

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
Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
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
Jayasurya Satheesh
  • 7,826
  • 3
  • 22
  • 39