如何将 table 分成 4 组?
How to split a table in 4 groups?
我有一个包含两列的 table。 Seq
列从 1 到 100。
Part_No Seq
A23 1
B88 2
C34 3
A43 4
B48 5
E11 6
A87 7
E64 8
...TILL Seq 100
我现在想拆分这个 table 如下所示:
Part_No_a Part No_b Part_No_c Part_No_d
A23 B88 C34 A43
B48 E11 A87 E64
我会使用带模函数的条件聚合:
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);
我有一个包含两列的 table。 Seq
列从 1 到 100。
Part_No Seq
A23 1
B88 2
C34 3
A43 4
B48 5
E11 6
A87 7
E64 8
...TILL Seq 100
我现在想拆分这个 table 如下所示:
Part_No_a Part No_b Part_No_c Part_No_d
A23 B88 C34 A43
B48 E11 A87 E64
我会使用带模函数的条件聚合:
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);