直接上题

有a表(虚构)
时间1  购买1   时间2   购买2
2001   10      2003    3
2004   3       2000    5
2003   5       2001    2
现在要进行统计得出如下结构:
时间  购买1  购买2 
求解 
不知道能不能解决

解决方案 »

  1.   

    用full join 试一下自我连接
    然后再加一点filter 条件
      

  2.   

    ok, you can create a test table which will help you come ture the concenquence
    First, 
    create table test as select 时间1 时间,购买1, 购买2 from <表的名称>;
    Second, 
    insert into test select 时间2,购买1, 购买2 from <表的名称>;Finally, 
     select * from test;
      

  3.   


    create table test(
           t1 int,
           g1 int,
           t2 int,
           g2 int
    )
    insert into test values(2001, 10, 2003, 3);
    insert into test values(2004, 3, 2000, 5);
    insert into test values(2003, 5, 2001, 2);select a.t1,a.g1,b.g2
    from test a, test b
    where  a.t1 = b.t2(+)
    union 
    select b.t2,a.g1,b.g2
    from test b, test a
    where  b.t2 = a.t1(+)
      

  4.   


    SQL> with tb as(
      2       select '2001' dt1,10 num1,'2003' dt2,3 num2 from dual union all
      3       select '2004',3,'2000',5 from dual union all
      4       select '2003',5,'2001',2 from dual)
      5  select nvl(a.dt1,b.dt2) dt,nvl(a.num1,0),nvl(b.num2,0)
      6    from tb a full join tb b
      7      on a.dt1=b.dt2
      8   order by dt
      9  /
     
    DT   NVL(A.NUM1,0) NVL(B.NUM2,0)
    ---- ------------- -------------
    2000             0             5
    2001            10             2
    2003             5             3
    2004             3             0
      

  5.   

    select a1.时间1,a1.购买1,a2.购买2 from a a1 full join a a2 on a1.时间1 = a2.时间2
      

  6.   

    如上面所说,full out join + 自连,就能解决问题。
      

  7.   

    with t as(
    select '2001' sj1,10 gm1,'2003' sj2,3 gm2 from dual
    union all
    select '2004',3,'2000',5 from dual
    union all
    select '2003',5,'2001',2 from dual
    )
    select sj1 "sj", sum(decode(kind, 1, gm1, 0)) "gm1", sum(decode(kind, 2, gm1, 0))  "gm2"
      from (select sj1, gm1, '1' kind
              from t
            union all
            select sj2, gm2, '2' from t)
     group by sj1;
    sj          gm1        gm2
    ---- ---------- ----------
    2000          0          5
    2001         10          2
    2003          5          3
    2004          3          0