Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select every nth row with NHibernate

How would you implement a query that selects every nth row, with NHibernate QueryOver, HQL or Criteria?

Currently I use the following T-SQL query:

SELECT * FROM (
    SELECT *, ROW_NUMBER() OVER (ORDER BY Id) AS [Row]
    FROM [TABLE_NAME]
) x WHERE (x.[Row] % 100) = 0

(Thanks to Marc Gravell)

like image 227
kshahar Avatar asked Nov 03 '22 14:11

kshahar


1 Answers

Have you considered the solution of using an indexing table in a cross join? What I mean is that you have a table with as many rows as you think you will need with an indexed column of integers going from 1-n in each row. This can be in a master database perhaps with a date column beside it - its amazing how useful this method is. The query would then look like

SELECT * FROM (
    SELECT *, ROW_NUMBER() OVER (ORDER BY Id) AS [Row]
    FROM [TABLE_NAME]
) x INNER JOIN [Index_Table] i ON i.Id*100=x.[Row]
like image 83
Dale M Avatar answered Nov 15 '22 05:11

Dale M