select bc.outid,bc.name,sum(t.opfare) from rec_cust_acc t,base_customers bc where 
t.acccode = 101 and t.customerid = bc.customerid and t.opdt between 
to_date(concat('2010-08-01',' 00:00:00'),'yyyy-mm-dd hh24:mi:ss') and 
to_date(concat('2010-12-06',' 23:59:59'),'yyyy-mm-dd hh24:mi:ss') 
group by bc.outid,bc.name order by bc.outid
我的需求是这样的:
这个sql查出来的是员工充值金额列表,我想在此基础上新添一项,用来显示补助金额
补助金额标准是这样的:
对于sql查出来的sum(t.opfare)值,如果>=50,补助金额为100;如果<50,补助金额为sum(t.opfare)*2
等待高手指点啊!

解决方案 »

  1.   

    具体怎么用啊,不太会啊,对Oracle不是很熟,你给写写吧O(∩_∩)O~
      

  2.   


    select bc.outid,bc.name,sum(t.opfare) , case when sum(t.opfare) >= 50 then 100 else sum(t.opfare) * 2 end "补助金额"
    from rec_cust_acc t,base_customers bc where  
    t.acccode = 101 and t.customerid = bc.customerid and t.opdt between  
    to_date(concat('2010-08-01',' 00:00:00'),'yyyy-mm-dd hh24:mi:ss') and  
    to_date(concat('2010-12-06',' 23:59:59'),'yyyy-mm-dd hh24:mi:ss')  
    group by bc.outid,bc.name order by bc.outid
      

  3.   

    查询下语法看看
    select bc.outid,
    bc.name,
    sum(t.opfare) as col, 
    case when sum(t.opfare)>=50 then 100 else sum(t.opfare)*2 end as col1 
    from rec_cust_acc t,base_customers bc where  
    t.acccode = 101 and t.customerid = bc.customerid and t.opdt between  
    to_date(concat('2010-08-01',' 00:00:00'),'yyyy-mm-dd hh24:mi:ss') and  
    to_date(concat('2010-12-06',' 23:59:59'),'yyyy-mm-dd hh24:mi:ss')  
    group by bc.outid,bc.name order by bc.outid
      

  4.   


    select bc.outid,bc.name,sum(t.opfare) , (case when sum(t.opfare) >= 50 then 100 else sum(t.opfare) * 2 end) buzhu
    from rec_cust_acc t,base_customers bc where 
    t.acccode = 101 and t.customerid = bc.customerid and t.opdt between 
    to_date(concat('2010-08-01',' 00:00:00'),'yyyy-mm-dd hh24:mi:ss') and 
    to_date(concat('2010-12-06',' 23:59:59'),'yyyy-mm-dd hh24:mi:ss') 
    group by bc.outid,bc.name order by bc.outid
      

  5.   


    select bc.outid,bc.name,sum(t.opfare),
    decode(sign(sum(t.opfare)-50),1,100,-1,sum(t.opfare)*2)
    from rec_cust_acc t,base_customers bc where  
    t.acccode = 101 and t.customerid = bc.customerid and t.opdt between  
    to_date(concat('2010-08-01',' 00:00:00'),'yyyy-mm-dd hh24:mi:ss') and  
    to_date(concat('2010-12-06',' 23:59:59'),'yyyy-mm-dd hh24:mi:ss')  
    group by bc.outid,bc.name order by bc.outid
      

  6.   

    select bc.outid,
           bc.name,
           sum(t.opfare),
           case
             when sum(t.opfare) >= 50 then
              100
             else
              sum(t.opfare) * 2
           end "补助金额"
      from rec_cust_acc t, base_customers bc
     where t.acccode = 101
       and t.customerid = bc.customerid
       and t.opdt between
           to_date(concat('2010-08-01', ' 00:00:00'), 'yyyy-mm-dd hh24:mi:ss') and
           to_date(concat('2010-12-06', ' 23:59:59'), 'yyyy-mm-dd hh24:mi:ss')
     group by bc.outid, bc.name
     order by bc.outid;
      

  7.   

    我还想在此基础上,添加sum(t.opfare)的合计,还有补助金额的合计
    sum(t.opfare)的合计是sum(sum(t.opfare))
    补助金额的合计怎么写啊?
      

  8.   

    嵌套也可以,单独写个合计的sql语句也行。
    帮帮忙吧,大侠!
      

  9.   


    --嵌套一层,用下分析函数
    select cnt.outid,
           cnt.name,
           sum(cnt.sumopfare) over(order by 1) totalopfare,
           sum(cnt.otherfee) over(order by 1) totalotherfee
      from (select bc.outid,
                   bc.name,
                   sum(t.opfare) sumopfare,
                   case
                     when sum(t.opfare) >= 50 then
                      100
                     else
                      sum(t.opfare) * 2
                   end otherfee
              from rec_cust_acc t, base_customers bc
             where t.acccode = 101
               and t.customerid = bc.customerid
               and t.opdt between
                   to_date(concat('2010-08-01', ' 00:00:00'),
                           'yyyy-mm-dd hh24:mi:ss') and
                   to_date(concat('2010-12-06', ' 23:59:59'),
                           'yyyy-mm-dd hh24:mi:ss')
             group by bc.outid, bc.name
             order by bc.outid) cnt
      

  10.   

    我只想在最后一行显示这两项合计,你这个sql查出来的是列表啊,好多项
      

  11.   

    最终需求:我想在最后一行统计出人数、充值金额总数、补助金额总数
    select count(distinct(bc.outid)),sum(sum(t.opfare)) from rec_cust_acc t,base_customers bc where 
            t.acccode = 101 and t.customerid = bc.customerid and t.opdt between 
            to_date(concat('2010-08-01',' 00:00:00'),'yyyy-mm-dd hh24:mi:ss') and 
            to_date(concat('2010-12-06',' 23:59:59'),'yyyy-mm-dd hh24:mi:ss') 
            group by bc.outid,bc.name,t.opfare order by bc.outid;
    这个sql能实现人数、充值金额总数的合计,补助金额的合计在此基础上怎么获得啊?
    帮忙啊!