V
V
VMesser2017-05-04 20:55:45
SQL
VMesser, 2017-05-04 20:55:45

Select first N records for each group, how does it work?

Faced with the classical task:
In the table one field - groups, another - values. You must select, say, three values ​​for each group. It is necessary to implement in Access.
There is a working query on google like this:

SELECT Группа, Значение
FROM Таблица
WHERE Значение IN
   (SELECT TOP 3 Значение
    FROM Таблица as Temp
    WHERE Temp.Группа= Таблица.Группа
    ORDER By Значение DESC)

The result is adequate, but I can't figure out how it works. According to the logic of things, the nested query returns 3 rows, which means that it can return no more than three Values. But at the output, I get the entire set of groups with a variety of values ​​\u200b\u200bgreater than three.
Please explain the math behind the query.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question