2
Good afternoon, fellow programmers.
I came across the following problem: I have two tables, one with applicants, and the other with vacancies. I need a query that crosses the two tables, and takes the first available vacancy and gives to the first requester, thus making a third table. I tried to do it in several ways, but every time I try, the result ends up giving me a third table with each requester taking all the vacancies in the table. (Requester 1 picks up all vacancies, then Requester 2 picks up all vacancies again, and so on)
The tables are a little extensive, so I will give only the main information for better understanding:
Applicants:
+----+-------------+----------+
| id | Solicitante | id vaga |
+----+-------------+----------+
| 01 | Joãozinho | 12345 |
| 02 | Mariazinha | 12345 |
| 03 | Bruno | 54321 |
| 04 | Celia | 54321 |
| 05 | André | 99999 |
+----+-------------+----------+
Vacancies:
+-----+---------+
| id | id vaga |
+-----+---------+
| 01 | 12345 |
| 02 | 54321 |
| 03 | 99999 |
| 04 | 99999 |
+-----+---------+
The answer I need is for him to return something like
+-------------+----------+
| Joãozinho | 12345 |
| Bruno | 54321 |
| André | 99999 |
+-------------+----------+
that is, allocate the available vacancies to the first applicants for these vacancies.
At the moment my query is like this:
SELECT C.Nome_Solic, C.Especialidade,P.Nome_Med,P.Data_Consul,P.Hora_Consul
FROM [DB_PMSP_CONSULTA].[dbo].[Tabela_Solicitante_HSPM] AS C
INNER JOIN [DB_PMSP_CONSULTA].[dbo].[Vagas_Disponiveis_HSPM] AS P ON
C.Especialidade = P.Espec
Would anyone have any idea how I can do what I need?
What is the criterion for assigning vacancy to an applicant (the smallest id first, alphabetical order, other selection criteria...)? And if there are not enough vacancies for all candidates or if there are not enough candidates for all vacancies, what do you expect the result?
– Diego Rafael Souza
The criterion of distribution of vacancies is, the first to request are with the available vacancies. If there are not enough vacancies, applicants who did not receive vacancies simply continue on the table waiting for new vacancy entries. If there are no candidates for vacancies, also, the vacancy is in the vacancy table expecting new query with new data. This system will constantly receive new vacancies and vacancies daily.
– Máttheus Spoo
According to their initial table John and Mary are disposing the vacancy 12345, but in its result. Mary took another vacancy (54321), this is correct?
– Caique Romero
I think it was an error in the example only. Ok, but what about each record in
Vagas
corresponds to the availability of a vacancy only or there may be a record there that could allocate 5 requesters, for example?– Diego Rafael Souza
Caique, it was a mistake in the example, sorry. Diego, there will be several vacancies for each type, and many more applicants per vacancy. (around 2,000 applicants for approximately 500 vacancies)
– Máttheus Spoo
This type of business rule is usually better implemented in a business rule layer rather than the BD. Note: if you use a wrong example you will end up with a wrong answer, edit your example
– jean
I already edited the example. , yes, the vacant ID will repeat itself. Vacancy tables will usually come with multiple repeated vague id 200~500 times in sequence. The key is in the first column in both cases
– Máttheus Spoo
For the result you expect need not even relate table the data are all in the first. I think there is something wrong in the concept.
– Caique Romero
How should I do then? Mind helping me with this?
– Máttheus Spoo
See if the help response.
– Caique Romero