select time,sum(money) from (select * from tab1,tab2)
group by time

解决方案 »

  1.   

    TO: bluelamb(bluelamb):你的语句运行不通过哦,说找不到time,和money,字段.可以替我再写一次嘛>?
    TO: ropriest(馆主):左右连接我知道,但在这语句中怎么用呀?麻你帮我写一下,好嘛??
    多谢!!!
      

  2.   

    select time,sum(money) from
    (
      select time,money from tb1 
        union
      select time,money from tb2
    )
    group by time
      

  3.   

    CodeMagic(ErrorDetector)的是正解,
      

  4.   

    select time,sum(money) money from
    (
      select time,money from tb1 
        union all
      select time,money from tb2
    )
    group by time
      

  5.   

    select time,sum(money) from
    (
      select time1 time,money1 money from a 
        union
      select time2 time,money2 money from b
    )
    group by time
      

  6.   

    select time,sum(money) from
    (
      select time1 time,money1 money from a 
        union all
      select time2 time,money2 money from b
    )
    group by time
      

  7.   

    不加all会滤掉两个表中,所有字段值都一致的记录。
    比如表2:
    time2  money2
    9月      45
    10月     10
    就能看出二者的区别了
      

  8.   

    想再请教一下:如果想由表1和表2得到如下的效果,SQL语句该如何写?
    表3:
    time    money1    money2
    9月       0         45
    10月      10        5
    11月      20        0
    12月      30        0 再次多谢高手们!!!分不够可以再加!
      

  9.   

    select time,sum(money1),sum(money2) from
    (
      select time1 time,money1, 0 money2 from a 
        union all
      select time2 time,0 money1, money2 from b
    )
    group by time
      

  10.   

    TO:onejune4450(中文字符) or eneryone :不要用union all可以实现吗?一来要连续查询两次,效率存在问题;二来我的表3还有其他字段(表1存在而表2不存在的字段)要显示,用union all这关键字实现存在问题.多谢!
      

  11.   

    select decode(time1,null,time2,time1) time,nvl(money1,0) money1,nvl(money2,0) money2 
    from temp_1
    full outer join temp_2 on (time1=time2)
    order by 2;
      

  12.   


    select time, sum(..),sum(..) from (   shine333(enihs) 上面的语句,可去掉 order by .. )group by time
      

  13.   

    select time,sum(money) money from 
    (
    select time,money from a
    union all
    select time,money from b
    ) group by time我经常这样用
      

  14.   

    TO:shine333(enihs),你语句上用到完全链接,number类型的字段我有用过完全链接,时间类型的字段可以进行链接嘛??