表head 字段year month  cust_id cust_name ,things,gettime(收货准时,0表示不准时,1表示准时),
要求查询到这些数据的同时,显示另外一行为 小计     收货的准时率(即gettime为1的次数  除以 记录的条数)
急求各位sql达人帮忙

解决方案 »

  1.   

    比如说我写一个sql查出来20条记录,那我就算这20条记录里面的准时收获率,在表的第21行显示出来
      

  2.   

    select year,month,cust_id,cust_name,sum(decode(收货准时,'1',1,0)) as 及时,count(1) as 总数 from 表名
    group by rollup (year,month,cust_id,cust_name)
      

  3.   


    select year,month,cust_id,cust_name, 
           nvl(gettime,sum(decode(gettime,1,1,0))/sum(1)) gettime  from head
    group by grouping sets((year,month,cust_id,cust_name,gettime),())
      

  4.   

    还有点小问题  我一共查出3行数据 gettime为0 0 1,最后得出的结果不是0.33333,而是0.048xxxx
      

  5.   

    with ta as (select 'a' a , 101 b, 0 c from dual union all
                select 'b' a , 101 b, 0 c from dual union all
                select 'c' a , 101 b, 1 c from dual ) 
                
                select a ,b ,nvl(c,sum(decode(c,1,1,0))/sum(1)) c1
                from ta group by 
                grouping sets((a,b,c),())
    ---------------------------------------------------
    1 a 101 0
    2 b 101 0
    3 c 101 1
    4 0.333333333333333
    ---------------------------------------------------
    要么发数据,要么慢慢找。