In order to summarize the comments, take a test sample of data from 100 consecutive numbers (we assume that they are not so consistent)
WITH dataset AS ( select 1 as id union all select id+1 from dataset where id < 100 ) select * from dataset;
Further, from among the ranking functions, we can use ntile(N)
, which divides the sample into N
groups. That is, to split 100 lines of 2, it is necessary to divide into 50 groups. For the general case, this number can be defined by a subquery:
SELECTid, ntile( (select count(id)/2 from dataset) ) over (order by id) FROM dataset;
and the result
id ----------- -------------------- 1 1 2 1 3 2 4 2 5 3 6 3 7 4
Or the second option is to use the function dense_rank()
, and for ranking, take the line numbers, dividing them completely into the desired number of elements in the group:
..... , nums AS ( SELECT id , (row_number() OVER (ORDER BY id) -1) AS rn FROM dataset ) SELECT id, dense_rank() OVER ( ORDER BY rn/2) FROM nums
ntile(50) over (order by id)
? - teran 7:16 pmdense_rank() over (order by rn/2)
, where rn is the line number? - teran