I need order every 10 row of a single column table and fetch the rows of n interval. Iam using where mod(rownum,10) for doing the second part. But i cant find a way to orderby every nth row before fetching the first and last row of n rows.Please help The table is like =>
Column
15
18
13
14
11
16
17
12
19
20
9
2
3
5
4
6
7
8
1
10
This is the query iam currently using==>
Select column
from (select column,row_number() over (order by column) as rn
from table
) t
where mod(rn,10)=0 or mod(rn,10)=1;
This will fetch
Column
1
10
11
20
But what i want is
Column
11
20
1
10
Ps: i cant orderby whole column then fetch every 1st and 10th row,i want to orderby first 10 column fetch 1st and 10th row then orderby next 10 colum fetch 11th to 20th etc..
And i can only fire the query one time only
Like this:
SELECT col, rn, (rn - 1) / 10 AS trunc
FROM (SELECT col, row_number() over (order by col) as rn
FROM data
) t
WHERE mod(rn,10)=0 or mod(rn,10)=1
ORDER BY (rn - 1) / 10 DESC
;
Result:
+------+----+-------+
| col | rn | trunc |
+------+----+-------+
| 11 | 11 | 1 |
| 20 | 20 | 1 |
| 1 | 1 | 0 |
| 10 | 10 | 0 |
+------+----+-------+