字段如下
id     name    num    price   
1      笔记本   1      5000
.......现在要查询占成本总价的80%物品明细(按照每个物品成本由高到底算起)

解决方案 »

  1.   

    最好给出完整的表结构,测试数据,计算方法和正确结果.否则耽搁的是你宝贵的时间。
    如果有多表,表之间如何关联?
    发帖注意事项
    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
      

  2.   


    --不知道搂主是不是这个意思
    --num*price大于等于总成本的80%
    --总成本等于 所有产品的num*price 之和
    select * from tb
    where num*price>=(select sum(num*price)*80 from tb)
      

  3.   

    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
      

  4.   


    ---猜的
    select a.* from tb a
    where a.num*a.price>=(select sum(num*price)*0.8 from tb b where a.name=b.name )
      

  5.   

    就比如id name num price   
    1 笔记本 1 20
    2  钢笔  2  10
    3  本子  1   30
    4  本子  1   30
    那么查询总成本(就是1*20+2*10+1*30+30=100)的80%(100*80%=80)的物品明细(按照每个物品成本由高到底算起)
    排序后的是
    id name num price  
    3  本子  1   30
    4  本子  1   30 
    1 笔记本 1 20
    2  钢笔  2  10
    那么80%(100*80%=80)的物品明细(按照每个物品成本由高到底算起)
    就是
    id name num price  
    3  本子  1   30
    4  本子  1   30 
    1 笔记本 1 20
      

  6.   

    随便取 只要num*price>总成本的80%吗? 毫无规律可言....
    id name num price   
    3 本子 1 30
    4 本子 1 30  
    1 笔记本 1 20
    2 钢笔 2 10
    5 笔记本 1 20
    如果是这样呢?
    怎么取?
      

  7.   


    -------不晓得你的本子和笔记本怎么归到一组的,我这里给你修改了数据。。
    SQL> 
    SQL> with tab as
      2  (
      3  select 1 id ,'笔记本' name,1 num, 20 price from dual union all
      4  select 2 id ,'钢笔' name,2 num, 10 price from dual union all
      5  select 3 id ,'笔记本' name,1 num, 30 price from dual union all
      6  select 4 id ,'笔记本' name,1 num, 30 price from dual
      7  )
      8  select k.id, k.name, k.num, k.price
      9    from (select t.*,
     10                 sum(price * num) over(partition by name order by 1) sumAmt,
     11                 sum(num * price) over(order by 1) totalAmt
     12            from tab t) k
     13   where sumAmt / totalAmt >= 0.8
     14   order by num * price desc
     15  ;        ID NAME             NUM      PRICE
    ---------- --------- ---------- ----------
             3 笔记本             1         30
             4 笔记本             1         30
             1 笔记本             1         20SQL>
      

  8.   


    SQL> 
    SQL> with tb as
      2  (
      3  select 1 id ,'笔记本' name,1 num, 20 price from dual union all
      4  select 2 id ,'钢笔' name,2 num, 10 price from dual union all
      5  select 3 id ,'本子' name,1 num, 30 price from dual union all
      6  select 4 id ,'本子' name,1 num, 30 price from dual
      7  )
      8  select id,name,num,price
      9  from (select id,name,num,price,sum(price*num) over(order by price desc) total
     10  from tb)
     11  where total<=(select sum(price*num)*0.8 from tb)
     12  /
     
            ID NAME          NUM      PRICE
    ---------- ------ ---------- ----------
             4 本子            1         30
             3 本子            1         30
             1 笔记本          1         20
      

  9.   


    --那么如果是这种情况的时候,只查到70%的
    id  name  num  price
    1   book1  1    10
    2   book2  1    20
    3   book3  1    30
    4   book4  1    40---------------------------
    with tab as 
    (
    select 1 as id, 'book1' name, 1 num, 10 price from dual
    union all
    select 2 ,'book2', 2, 20 from dual
    union all
    select 3 ,'book3', 1, 30 from dual
    union all
    select 4 ,'book4', 1, 40 from dual
    )
    select id,name,num,price
    from (select id,name,num,price,sum(num*price) over(order by price desc) total from tab)
    where total <=
      (select sum(price * num) * 0.8 from tab)
    -----------------------------
    4 book4 1 40
    3 book3 1 30--以上,只能查出%70的明细,可以不?