I have a table with a column to
, from
and ID
. I need to select only rows with every 10th ID
which is from A
to B
.
Asked
Active
Viewed 588 times
1

TT.
- 15,774
- 6
- 47
- 88

lokesh kumar
- 961
- 1
- 10
- 18
-
2If your DBMS supports Windowed Aggregate Function you can simply use a `ROW_NUMBER` modulo 10 – dnoeth Jan 19 '16 at 09:44
-
What's your DBMS, does it support ROW_NUMBER? – dnoeth Jan 19 '16 at 09:45
-
Which DBMS you are using? – Ankit Bajpai Jan 19 '16 at 09:47
-
then it's ok for ROW_NUMBER you can just go like dnoeth said – Raffaello.D.Huke Jan 19 '16 at 09:57
2 Answers
4
select * from
(select * from table where from = 'A' and to ='B' order by ID)
where mod(rownum/10,1) = 0
It first takes only those from 'A' to 'B', then giving them rownums, and selecting only those in the 10th 20th ETC places..

sagi
- 40,026
- 6
- 59
- 84
-
1You have to use order by: (select * from table where from = 'A' and to ='B' ORDER BY ID) – Jean-Christophe Blanchard Jan 19 '16 at 10:10
-
-
@sagi its not working..I found something in another post http://stackoverflow.com/questions/15963129/how-to-use-rownum – lokesh kumar Jan 20 '16 at 09:56
-1
Or you can just use
select from, to, id
from table1
where id like'%0' and from ='A' and to = 'B'

Dmitriy
- 5,525
- 12
- 25
- 38

Raffaello.D.Huke
- 552
- 2
- 9
-
Who said the ID's are continuously ? he wants the 10th not id = 10 and id = 20 – sagi Jan 19 '16 at 10:01