Skip to content
Advertisement

SQL: how to select distinct values with an offset

Given the following table:

id      value     
------- -------
0       1      
1       6      
2       7      
3       9      
4       2      
5       20     
6       21      
7       3  

I’m wondering if there’s a way to select distinct rows where there’s a difference between (integer) values of (for example) 3, giving a priority to id (low id value = more prioririty). Programmatically, I would iterate over values starting from id=0 and discarding rows where value, compared to my current list, has a difference less than 3. Here follows an example in C to give an idea:

Is it possible to do this filtering in SQL?

The result, given the initial table, would be:

id      value     
------- -------
0       1      
1       6
3       9      
5       20 

Advertisement

Answer

You can do it with a recursive CTE:

See the demo.
Results:

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