Good afternoon, fellow programmers.
I came across the following problem: I have two tables, one with applicants, and one with vacancies. I need a query that crosses the two tables, and get the first available slot and give it to the first requester, thus doing a third table. I tried to do it in several ways, but whenever I try, the result ends up giving me a third table with each applicant picking up all the vacancies in the table. (applicant 1 takes all vacancies, then applicant 2 takes all vacancies again, and so on)
The tables are a bit long, so I'll just give the main information for better understanding:
Seekers:
+----+-------------+----------+
| id | Solicitante | id vaga |
+----+-------------+----------+
| 01 | Joãozinho | 12345 |
| 02 | Mariazinha | 12345 |
| 03 | Bruno | 54321 |
| 04 | Celia | 54321 |
| 05 | André | 99999 |
+----+-------------+----------+
Jobs:
+-----+---------+
| id | id vaga |
+-----+---------+
| 01 | 12345 |
| 02 | 54321 |
| 03 | 99999 |
| 04 | 99999 |
+-----+---------+
The answer I need is for it to return something like
+-------------+----------+
| Joãozinho | 12345 |
| Bruno | 54321 |
| André | 99999 |
+-------------+----------+
That is, to assign the vacancies available to the first applicants to these vacancies.
At the moment my query looks 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?