使用connect by rownum<=n可以复制N条重复数据,但是如何分组复制重复数据呢?
比如有张表t,field表示显示的字段,num表示重复的次数field     num
 A         2
 B         3
 C         1有N条数据SELECT XXXX from t xxxx
要得到的结果为
field
 A
 A
 B
 B
 B
 C
.....

解决方案 »

  1.   

    select a.field
    from t a,t b
    where a.field=b.field
    connect by rownum<=b.num
    ;
      

  2.   

    这个无法实现,只会把第一条记录重复num遍,刚测试了下connect by rownum的原理,好像不能用这个方法实现
      

  3.   


    with t(field,num) as(
    select 'A',2 from dual
    union select 'B',3 from dual
    union select 'C',1 from dual
    )
    select regexp_substr((select wm_concat(rpad(field,num*2-1,','||field))
    from t),'[^,]+',1,rownum) result from dual connect by rownum<=(select sum(num) from t);
    /*
    RESULT                                                                          
    --------------------------------------------------------------------------------
    A                                                                               
    A                                                                               
    B                                                                               
    B                                                                               
    B                                                                               
    C                                                                               已选择6行。
    */
      

  4.   

    select a.field
    from t a,(select rownum rn from dual connect by rownum<=(select max(num) from t))
    where a.num>=rn
    ;
      

  5.   

    这里果然人才济济啊,canhui87的方法不错,zhangandli的方法不够通用,可能我的需求没说清楚,我最终需要的是要关联其他表,按次数复制整条数据,而不是某个字段
    谢谢大家了
      

  6.   

    #include"yuchuli.h"
    #define LIST_INIT_SIZE 100
    #define LISTINCREMENT 10
    typedef struct
    {
    ElemType *elem;
    int  length;
    int  listsize;
    }SqList;#include"yuchuli.h"
    #include"stdafx.h"
    #include"jiguot.h"
    #include"malloc.h"
    #include"stdlib.h"Status InitList_Sq(SqList &L)
    {
    int OVERFLOW;
    L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
    if(!L.elem)exit(OVERFLOW);
    L.length = 0;
    L.listsize = LIST_INIT_SIZE;
    return 0;
    }
    #include"stdafx.h"
    #include"jiguot.h"
    #include"malloc.h"
    #include"stdlib.h"
    Status ListInitList_Sq(SqList &L, int i, ElemType e)
    { int *q=NULL,*p=NULL;
    int ERROR,*newbase,OVERFLOW;
    if(i<1||i>L.length+1)return ERROR;
    if(L.length>=L.listsize)

    newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
    if(!newbase)exit(OVERFLOW);
    L.elem=newbase;
    L.listsize +=LISTINCREMENT;
    }
    q=&(L.elem[i-1]);
    for(p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;
    *q = e;
    ++L.length;
    return 0;
    }
    #include "stdafx.h"
    #include"stdio.h"
    #include"jiguot.h"
    #include"malloc.h"
    #include"stdlib.h"
    #define N 5
    Status ListInitList_Sq(SqList &L, int i, ElemType e);
    Status InitList_Sq(SqList &L);int _tmain(int argc, _TCHAR* argv[])
    {
    int a[N],c,x,i=0;
    SqList List={NULL,0, 0};
    SqList L;
    if(InitList_Sq( List))
    {
    printf("请输入五个数:\n");
    for(c=1;c<N;c++)
    {
    scanf("%d",&a[c]);
    }
    }
    printf("请输入插入的位置,插入的数:\n");
    scanf("%d\n%d",&i,&x);
    InitList_Sq(List, i, x);
    printf("%d",List.elem[0]);
    int m;
    scanf("%d",&m);
    return 0;
    }在一个线性表中插入一个数
    主函数为什么会出现InitList_Sq(List, i, x);  不能接受3个参数??
     求解