Skip to content
Advertisement

How to get every nth row value in sql

I have a select statement in sql:

SELECT DISTINCT [Year] FROM [data_list] ORDER BY [YEAR] ASC;

Result:

Year
----
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028

I want list of year in 3 increments. For example:

Year
----
2019
2022
2025
2028

Advertisement

Answer

You can use ROW_NUMBER:

SELECT [Year] 
FROM (
  SELECT [Year], ROW_NUMBER() OVER (ORDER BY [Year] ASC) rn 
  FROM table_name
  GROUP BY [YEAR]
) t WHERE (t.rn - 1) % 3 = 0

demo on dbfiddle.uk

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement