How to split a table in 4 groups?

452 Views Asked by At

I have a table with two columns. The Seq column goes from 1 to 100.

Part_No             Seq
A23                  1
B88                  2
C34                  3
A43                  4
B48                  5
E11                  6
A87                  7
E64                  8
...TILL Seq 100

I now want to split this table like below:

Part_No_a             Part No_b            Part_No_c            Part_No_d
A23                     B88                  C34                   A43
B48                     E11                  A87                   E64
1

There are 1 best solutions below

2
On BEST ANSWER

I would use conditional aggregation with the modulo function:

select max(case when seq % 4 = 1 then part_no end) as part_no_a,
       max(case when seq % 4 = 2 then part_no end) as part_no_b,
       max(case when seq % 4 = 3 then part_no end) as part_no_c,
       max(case when seq % 4 = 0 then part_no end) as part_no_d
from databasetable t
group by ((seq - 1) / 4);