CTE
with ROW_NUMBER()
;WITH CTE AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY codigo_curso ORDER BY data_ingresso) AS row
FROM ALUNOS
)
SELECT *
FROM CTE
WHERE row= 1
SubQuery
Correlated:
SELECT l.*
FROM
(SELECT ALUNOS.*,
ROW_NUMBER() OVER (PARTITION BY codigo_curso
ORDER BY data_ingresso) AS linha
FROM ALUNOS) l
WHERE l.linha = 1;
Update
Today I learned a different way of making a query that will generate the same result.
SELECT *
FROM ALUNOS
INNER JOIN(SELECT codigo_curso, MIN(data_ingresso) data_ingresso
FROM ALUNOS
GROUP BY codigo_curso) A
ON ALUNOS.codigo_curso = A.codigo_curso
AND ALUNOS.data_ingresso = A.data_ingresso
- Take the smallest
data_ingresso
grouped by codigo_curso
- Make a
JOIN
with that result.
You meant to SQL:
Return students who first joined a certain course.
- PARTITION BY value_expression -
value_expression
specifies the column by which the result set is partitioned.
- Order_by_clause - Determines the orders in which lines will be assigned.
Row Number
in a nutshell will assign an order to your lines.
Recommended Readings:
It worked, I only had to take the down, because the question wanted the student who entered first, so the student that the date of entry was the lowest, so the ordination must be of ascending order
– M.Amaral
You’re right, Desc reverses ordering. I’ll edit the answer
– Alexandre Cavaloti