Quantcast
Viewing all articles
Browse latest Browse all 35

Answer by FoxArc for How to select the nth row in a SQL database table?

If you want to look at native functionalities: MySQL, PostgreSQL, SQLite, and Oracle (basically SQL Server doesn't seem to have this function) you could ACTUALLY use the NTH_VALUE window function.Oracle Source: Oracle Functions: NTH_VALUE

I've actually experimented with this in our Oracle DB to do some comparing of the first row (after ordering) to the second row (again, after ordering).The code would look similar to this (in case you don't want to go to the link):

SELECT DISTINCT dept_id     , NTH_VALUE(salary,2) OVER (PARTITION BY dept_id ORDER BY salary DESC           RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)         AS "SECOND HIGHEST"     , NTH_VALUE(salary,3) OVER (PARTITION BY dept_id ORDER BY salary DESC           RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)        AS "THIRD HIGHEST"  FROM employees WHERE dept_id in (10,20) ORDER     BY dept_id;

I've found it quite interesting and I wish they'd let me use it.


Viewing all articles
Browse latest Browse all 35

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>