Split Result from SQL Query

For example this SQL statement returns 1k rows SELECT * FROM tableName WHERE someCondition

Split Result from SQL Query

  1. Question by mKorbel
  2. Answer by Milovan Tomašević

Question by mKorbel

For example this SQL statement returns 1k rows

SELECT * FROM tableName WHERE someCondition

my question is: is it possible by using WHERE

1) Split Resultset to the 10 partial resultsets, then

  • 1st. would be returns 0% - 10%,

  • 2nd. 10% - 20%,

  • etc..

2) cuts range between 50 - 150th rows

Answer by Milovan Tomašević

my question is: is it possible by using WHERE

My answer is: yes, it is possible.

One of the solutions would be to use the MOD function. The MOD function returns the remainder of one number divided by another and the function accepts two arguments:

MOD(dividend,divisor)
  • dividend is a literal number or a numeric expression to divide.
  • divisor is a literal number or a numeric expression by which to divide the dividend.

In particular, dividing the data into ten parts, in your example, would look like this:

SELECT * FROM tableName WHERE mod(INDEX_ID, 10) = 1
SELECT * FROM tableName WHERE mod(INDEX_ID, 10) = 2
SELECT * FROM tableName WHERE mod(INDEX_ID, 10) = 3
SELECT * FROM tableName WHERE mod(INDEX_ID, 10) = 4
SELECT * FROM tableName WHERE mod(INDEX_ID, 10) = 5
SELECT * FROM tableName WHERE mod(INDEX_ID, 10) = 6
SELECT * FROM tableName WHERE mod(INDEX_ID, 10) = 7
SELECT * FROM tableName WHERE mod(INDEX_ID, 10) = 8
SELECT * FROM tableName WHERE mod(INDEX_ID, 10) = 9
SELECT * FROM tableName WHERE mod(INDEX_ID, 10) = 0

INDEX_ID is index from table_name.


Improve this page: 

Share on:      

Comments 💬