现在我要实现一个存储过程,
存储过程有2个参数,一个是组个数,一个是组Id
Test_Proc(GourpNum number,GroupsId varchar2)
如:Test_Proc(3,"g1,g2,g3")
分为3组,每组的标识分别为g1、g2、g3将A表
A表的数据结构如下
Num
test1
test2
test3
.......
testN现在把A表的数据平均分到B表,比如A表现在有100条数据我要分3组则两个组有33,一个组为33其他平均分组一样的
组50条数据
Num   groupId
test1 g1
test2 g1
test3 g1
.......
test50 g2
test51 g2
test52 g2
.......

解决方案 »

  1.   

    是要平均分组吗,直接用ntile分析函数with a as
    (select 'test'||rownum num
    from dual
    connect by rownum<=10
    )
    select num
    ,ntile(3) over(order by num ) groupid
    from a
    ;
    NUM                                             GROUPID
    -------------------------------------------- ----------
    test1                                                 1
    test10                                                1
    test2                                                 1
    test3                                                 1
    test4                                                 2
    test5                                                 2
    test6                                                 2
    test7                                                 3
    test8                                                 3
    test9                                                 3