I have a simple table in SQL Server.
id | OtherId | createdAt |
---|---|---|
1 | A | 2019-11-28 05:03:03.983 |
2 | A | 2019-11-28 05:03:06.937 |
3 | B | 2019-11-28 05:03:12.983 |
I want to retrieve a timestamp when the number of rows becomes equal to or greater than a fixed number, let us say 10, where OtherId
is A
.
So, if the number of rows with A
in the OtherId
column is more than 10, then return the value from createdAt
.
Best Answer
You could try along
The inner
SELECT
groups the records by OtherId, orders and ranks them by createdAt. The outerSELECT
picks the record by some number.The CTE (common table expression) @Akina mentioned, would pull the sub-select to the top:
Do not see much difference in this case. But the technique helps a lot for more complex settings as it allows to grow the statements and is usually more readable.
If, however, only a single OtherId is of interest, there is no need for the window function:
On the other hand: Returning either a specific occurrence or the maximum could be done via a
UNION
:See it in action: db<>fiddle
Please comment, if and as this requires adjustment / further detail.