有一张销售表如下:
user   account
001    100.00
002    34.00
003    89.00
005    130.00
006    634.00
007    689.00
......
......现在需要计算一个数据:
找出满足条件 account总和大于等于10000的第一个user。有什么一些简单的方法。

解决方案 »

  1.   

    Select Top 1 user,account From Table Where account > 10000不是这样吗?
      

  2.   

    select sum(account) as total,[user] from xxx group by [user]
    上面根据user汇总,然后你从里面选出来就是了
      

  3.   

    写错了。是:account >= 10000
    大于等于
      

  4.   

    -0- 没看懂 是不是要对user分组啊?
      

  5.   

    select sum(account) as total,[user] from xxx group by [user] having sum(account)>=1000 order by user asc
      

  6.   

    select top 1 v.* from (
    select sum(account) as total,[user] from xxx group by [user]) v order by v.total asc
      

  7.   


    select top 1 * from 
    (
      select  sum(account) as total,[user] from 你的表名 group by [user] where total>=10000
    );
      

  8.   

    是sum(account) >= 10000的第一条记录
      

  9.   

    +1
    论坛签名======================================================================PCI_E:你好!
    截至 2011-10-21 16:54:54 前:
    你已发帖 27 个, 未结贴 1 个;
    结贴率为: 96.30%

    当您的问题得到解答后请及时结贴.

    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖如何给自己的回帖中也加上签名?
    http://blog.csdn.net/q107770540/archive/2011/03/15/6250007.aspx
      

  10.   

    能这样写么?
    这样写吧
    select top 1  sum(account) as total,[user] from 销售表 group by [user] having SUM(account)>10000
      

  11.   

    select sum(account) as total,[user] from 你的表名 group by [user] where total>=10000
    语句不对吧
    where条件怎么能在group by语句后面?
      

  12.   

    应该可以的 要加个
    select top 1 * from
    (
    select sum(account) as total,[user] from 你的表名 group by [user] where total>=10000
    ) a;
      

  13.   

    对。必须要,不然提示错误了。
    论坛签名======================================================================PCI_E:你好!
    截至 2011-10-21 16:54:54 前:
    你已发帖 27 个, 未结贴 1 个;
    结贴率为: 96.30%

    当您的问题得到解答后请及时结贴.

    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
    http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
    http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html如何给分和结贴?
    http://community.csdn.net/Help/HelpCenter.htm#结帖如何给自己的回帖中也加上签名?
    http://blog.csdn.net/q107770540/archive/2011/03/15/6250007.aspx
      

  14.   

    select top 1 *
    from
    (
    select sum(pcount) as total,fileid from #temp_picturedetail
    group by fileid
    having sum(pcount) >= 100
    ) a