大家好,问一个sql 的select的问题,比如select 出来的项有
"区间通话费(电信)","区间通话费(联通)","区间通话费(移动)",
"国内长途费(移动)","国内长途费(联通)",11816888市话费........
怎么把区间通话费的相关数据合并为一项,区内通话费的相关数据合成一项,长途费合成一项呢?每条记录有两个字段,还有一个是话费,意思是要分类求出话费总数谢谢,在线等!

解决方案 »

  1.   

    select 区间通话费(电信)+区间通话费(联通)+区间通话费(移动) as 区间通话费,国内长途费(移动)+国内长途费(联通) as 长途费,11816888市话费 as 市话费 from tab
      

  2.   

    select sum(话费) group by .....
    group by 的东西根据你的需求,在字段中提取吧.
      

  3.   

    呵呵,对于第一种   chd2001(天蝎降临--千分散尽还复来):不知道它的话费类型有多少种。
    第二种:  jdsnhan(柳荫凉)  请具体给出group by 中的提取方法
      

  4.   

    原始数据说清除一些,"区间通话费(电信)","区间通话费(联通)","区间通话费(移动)",
    "国内长途费(移动)","国内长途费(联通)",11816888市话费 如果能直接查出来
    那就可以像 chd2001 所说直接相加, 如果需要分类 就再加上 group by .. ,现在不知道你原始数据就很难分析怎么分类.
      

  5.   

    SELECT TYPE,sum(charg)
    from tablename
    group by typelz是要这样的结果么
      

  6.   

    select 区内通话费  
            ,区间通话费 
            ,国内长途费 
           ,国内IP长途费
    from(
    select type,sum(decode(substr(type,0,5),'区内通话费',charg,0)) 区内通话费
           ,sum(decode(substr(type,0,5),'区间通话费',charg,0)) 区间通话费
           ,sum(decode(substr(type,0,5),'国内长途费',charg,0)) 国内长途费
           ,sum(decode(substr(type,0,5),'国内IP长途',charg,0)) 国内IP长途费
      from tab
    group by type)
      

  7.   

    tongyu10068() ,呵呵,你给我方法我也想过了,但是这个类型不知道有哪些啊
      

  8.   

    type只是分组条件,用不着知道有哪些吧?
      

  9.   

    ,sum(decode(substr(type,0,5),'国内IP长途',charg,0)) 国内IP长途费  ,还有一些呢,比如 11816888市话费,还有一些我们不知道的啊
      

  10.   

    to:楼主
    >类型不知道有哪些啊
    应该是最短分组的‘类型’不知有哪些,这个要求就是设计/业务人员必须要调研的了。1、必须得确定出业务需求的最短‘类型’,之后确定这种最短‘类型’的长度,作为substr要取得的长度。
      如最短‘类型’的长度=5,sql如下:
      select substr(t.type, 1, 5) as 类型,
           sum(charg) as 费用
      from tab t
      group by substr(t.type, 1, 5)
    2、确定出业务需求的最短‘类型’,可以通过显示‘类型’的种类来人为推断。
      sql如下:
      select distinct t.type  as 全部类型
      from tab t
      order by t.type  
    3、如果最短‘类型’也不止一种,就要人为判断出几种,之后按种类分组,查询后再union;
      
      

  11.   

    tongyu10068() 给出的解决方案,运行出来的结果是:
    区内通话费 区间通话费 国内长途费 国内IP长途费 11816888市话费
    0 0 0 0 22
    0 0 0 1798 0
    0 0 33 0 0
    0 0 66 0 0
    0 0 44 0 0
    0 253 0 0 0
    0 12 0 0 0
    60 0 0 0 0
    2773 0 0 0 0
    1713 0 0 0 0
    88 0 0 0 0
    275 0 0 0 0
    还是没有求出费用总和,比如区内通话费,应该结果是 60+2773+1713+88+275
      

  12.   

    楼主是新手吧!
    把tongyu10068() 的内层sql的select 和group by 改一下:
    select type
           ,sum(decode(substr(type,0,5),'区内通话费',charg,0)) 区内通话费
           ,sum(decode(substr(type,0,5),'区间通话费',charg,0)) 区间通话费
           ,sum(decode(substr(type,0,5),'国内长途费',charg,0)) 国内长途费
           ,sum(decode(substr(type,0,5),'国内IP长途',charg,0)) 国内IP长途费
      from tab
    group by type
    --------------->
    select substr(type,0,5)
           ,sum(decode(substr(type,0,5),'区内通话费',charg,0)) 区内通话费
           ,sum(decode(substr(type,0,5),'区间通话费',charg,0)) 区间通话费
           ,sum(decode(substr(type,0,5),'国内长途费',charg,0)) 国内长途费
           ,sum(decode(substr(type,0,5),'国内IP长途',charg,0)) 国内IP长途费
      from tab
    group by substr(type,0,5)
      

  13.   

    licsth() :你这个group by substr(type,0,5)好像不行啊
      

  14.   

    咋不行?内层的select 后面也要substr(type,0,5) 的!
    select 区内通话费  
            ,区间通话费 
            ,国内长途费 
           ,国内IP长途费
    from(
    select substr(type,0,5)
           ,sum(decode(substr(type,0,5),'区内通话费',charg,0)) 区内通话费
           ,sum(decode(substr(type,0,5),'区间通话费',charg,0)) 区间通话费
           ,sum(decode(substr(type,0,5),'国内长途费',charg,0)) 国内长途费
           ,sum(decode(substr(type,0,5),'国内IP长途',charg,0)) 国内IP长途费
      from tab
    group by substr(type,0,5)
    )