例如
no(非主键)        m
1                 100
2                 100
2                 200
1                 100
2                 100求出 sum(m)> 300 的记录来
要最效率的

解决方案 »

  1.   

    select no, m from (select no, sum(t.m) sum_m from ***** group by no) where sum_m > 500
      

  2.   

    select no,m
    from(
    select no,m ,,sum()over(partition by no order by no)count
    from table_name
    )
    where count>300
      

  3.   


    select no,m
      from (select no,m,sum(m)over(partition by no) sm from table_name)
     where sm > 300;
      

  4.   


    with ta as
    (
    select 1 as no ,100 as m from dual
    union all
    select 2 as no ,100 as m from dual
    union all
    select 2 as no ,200 as m from dual
    union all
    select 1 as no ,100 as m from dual
    union all
    select 2 as no ,100 as m from dual
    )
    select * from 
    (
    select no,m,sum(m)over(partition by no) as num from taorder by dbms_random.value()
    ) where num>300;
      

  5.   

    select *
    from table1 a
    where (select sum(m) from table1 where no=a.no)>300
      

  6.   

    select no,mfrom (select no,m,sum(m)over(partitionby no) smfrom table_name)where sm>300; 
      

  7.   

    select no,m 
    from( 
    select no,m ,,sum()over(partition by no order by no)count 
    from table_name 

    where count>300
      

  8.   

    这是道面试题 其实需求不是很难 但是我不知道如何判断是不是最有效率的办法  大概要多少的数据量可以明显看出sql的效率差别来?还有开窗函数是oracle特有的吧   要求只用普通的sql   不要用到专有的分析函数什么的
      

  9.   

    select no,sum(m) from table_name  group by no having sum(m) > 300
      

  10.   


    似乎不对啊这里只有no=1的,sum(m)》300,查询后的结果是:
    no      m
    1       340
    1       370
    1       390
    怎么得出了3行了,正常应该就是一行
    no      m
    1       3901
      

  11.   

    --请仔细确认你的数据,如果里面有空值 用下面这个,最好把你测试的数据拿上来,要不没法分析。
    select no,m
      from (select no,m,sum(nvl(m,0))over(partition by no) sm from table_name)
     where sm > 300;
      

  12.   

    select no,sum(m) from table_name  group by no having sum(m) > 300
    这个效率最高