Reputation: 323
I have a SQL statement
SELECT COUNT(appl.applicantID)
FROM course_Batch pD
INNER JOIN applicant appl ON pD.programmeID = appl.programmeID
GROUP BY pD.programmeID
So, in my applicant
table, I've 2 applicants tied to a record
in my course_Batch
.
However, when I use that SQL statement I pasted above, I get 4. Because in my course_Batch
table, I've multiple records under 1 programme ID
.
ProgrammeDetail
table with sample data:
-------------------------------------------
| programmeID | programmeRequirement |
-------------------------------------------
| P01 | 20 |
| P01 | 20 |
-------------------------------------------
Applicant
table with sample data:
-----------------------------------
| applicantID | programmeID |
-----------------------------------
| A001 | P01 |
| A002 | P01 |
-----------------------------------
Is there a way to achieve this?
---------------------------------------------------------------
| programmeID | programmeRequirement | applicantCount |
---------------------------------------------------------------
| P01 | 20 | 2 |
---------------------------------------------------------------
Upvotes: 0
Views: 52
Reputation: 4765
Use INNER JOIN
. It will give your expected result
SELECT
programmedetail.programmeID,
programmedetail.programmeRequirement,
COUNT(
DISTINCT (applicant.applicantID)
) AS applicantCount
FROM
programmedetail
INNER JOIN applicant ON programmedetail.programmeID = applicant.programmeID
GROUP BY
programmedetail.programmeID,
programmedetail.programmeRequirement;
By clicking on the below link you can see your expected result in live which you want.
Upvotes: 1