I need help to write a simple procedure. Let me explain what I’m trying to do.
I have 3 tables
- tJobOffer
- tApplication
- tApplicationStatus
I would like to create a procedure that return me a list of tJobOffer with the number of candidate that responded to this tJobOffer. Candidate is a status of my table tApplicationStatus. This table is linked to tApplication that is linked to tJobOffer. An application can be CANDIDATE / ACCEPTED / REFUSED / IGNORED / …
I created this query :
SELECT
[T].[JobOfferId],
[T].[JobOfferTitle],
COUNT([A].[ApplicationId]) AS [CandidateCount]
FROM [tJobOffer] AS [T]
LEFT JOIN [tApplication] AS [A]
ON [A].[JobOfferId] = [T].[JobOfferId]
LEFT JOIN [tApplicationStatus] AS [S]
ON [S].[ApplicationStatusId] = [A].[ApplicationStatusId]
AND [S].[ApplicationStatusTechnicalName] = 'CANDIDATE'
GROUP BY
[T].[JobOfferId],
[T].[JobOfferTitle]
--[A].[ApplicationStatusId]
ORDER BY [T].[JobOfferTitle]
The result is
> 52ED7C67-21E1-49BB-A1F8-0601E6EED1EA Annonce 1 0
> F26B228D-0C81-4DA8-A287-F8F997CC1F9C Annonce 1b 0
> 9DA60B23-F113-4C7F-9707-2B90C1556D5D Announce 25 2
> 258E11A7-79C1-47B6-8C61-413AA54E2360 Announce 3 0
> DA582383-5DF4-4E1D-837C-382371BDEF57 Announce 6 2
This is not correct because I only have 1 candidate for Announce 6. If a set my line
--AND [S].[ApplicationStatusTechnicalName] = 'CANDIDATE'
in comment the result is the same. My query seems to ignore this line. What is wrong?
EDIT —
My correct result should be
> 52ED7C67-21E1-49BB-A1F8-0601E6EED1EA Annonce 1 0
> F26B228D-0C81-4DA8-A287-F8F997CC1F9C Annonce 1b 0
> 9DA60B23-F113-4C7F-9707-2B90C1556D5D Announce 25 2
> 258E11A7-79C1-47B6-8C61-413AA54E2360 Announce 3 0
> DA582383-5DF4-4E1D-837C-382371BDEF57 Announce 6 1
I guess you want this:
You could also join first the 2 “Application” tables, group by and then join the derived table with
JobOffer: