1. select kcode,max(kmoney) as kmoney
   from (  select top 100 * from tablename ) a
   group by kcode2. select kcode,
       sum(case when ktype='yes' then kmoney*konum else kmoney end) knew_money
   from (  select top 100 * from tablename ) a
   group by kcode

解决方案 »

  1.   

    ----First
    select a.kCode,[kMoney]=sum(a.kMoney) 
    from (select top 100 * from test) a
    group by a.kCode
    ----Second
    select a.kCode,
           [knew_money]=sum(a.kMoney*(case a.kType when 'no' then 1 else  a.kOnum end)) 
    from (select top 100 * from test) a
    group by a.kCode
      

  2.   

    ----如果加上汇总
    select a.kCode,
           [knew_money]=sum(a.kMoney*(case a.kType when 'no' then 1 else  a.kOnum end)) 
    from (select top 100 * from test) a
    group by a.kCode
    union all
    select '汇总',
           [knew_money]=sum(a.kMoney*(case a.kType when 'no' then 1 else  a.kOnum end)) 
    from test a
      

  3.   

    是这个意思吗? 你的数据有问题?
    ----First
    select top 100 b.* 
    from (select a.kCode,[kMoney]=sum(a.kMoney) from test a group by a.kCode) b
      

  4.   

    to  victorycyz(中海) 
    按你的 1 SQL 所执行的结果。
    kcode    kmoney
    -----------------
    1030204 .00
    130121 6560.00
    2090087 1896.00
    2090092 286.00
    nokia .00不对的呀!
      

  5.   

    ----First  是这个意思吗?
    select a.kCode,
           [kMoney]=sum(a.kMoney) into #tmpTab from test a 
           group by a.kCode 
           order by kMoney desc
    select top 100 * from #tmpTab
      

  6.   

    to  shuichangliu 你的结果如下:kcode    kmoney
    -----------------
    1030204 .00
    130121 6560.00
    2090087 1896.00
    2090092 286.00
    nokia .00也不是我想要的!根据 kmoney 的汇总进行降序排列,而不是 kcode !!!
      

  7.   

    select a.kCode,
           [kMoney]=sum(a.kMoney*(case a.kType when 'no' then 1 else  a.kOnum end))
           into #tmpTab
           from test a
           group by a.kCode 
           order by kMoney desc
    select top 100 * from #tmpTab
    union all
    select '汇总',
           [knew_money]=sum(a.kMoney*(case a.kType when 'no' then 1 else  a.kOnum end)) 
    from test a
      

  8.   

    第一个查询
    select a.kcode,sum(kmoney) as kmoney from (select top  100 * from tbl) a
    group by a.kcode第二个查询
    select a.kcode,
    [knew_money]=sum(a.kmoney*(case a.ktype
    when 'no' then 1
    else cast(a.konum as numeric(10,2))
    end))
    from (select top 100 * from tbl ) a
    group by a.kcode
      

  9.   

    请恕我开始没说清:根据 kmoney 的汇总进行降序排列,而不是 kcode !!!
      

  10.   

    在加上个order by 不就可以了
      

  11.   

    不行的呀:
    kcode    kmoney
    -----------------
    130121 6560.00
    2090087 1896.00
    2090092 286.00
    nokia .00
    1030204 .00
      

  12.   

    不行的呀:
    kcode    kmoney
    -----------------
    130121 6560.00
    2090087 1896.00
    2090092 286.00
    nokia .00
    1030204 .00
    这样还不行?你要怎么样的?
      

  13.   

    to  victorycyz:kcode    kmoney
    -----------------
    130121 6560.00
    2090087 1896.00
    2090092 286.00
    nokia .00
    1030204 .00我每次测试都是提取 TOP 5 的记录,而我的记录近有5000个记录,排在前5位怎么会为0??
      

  14.   

    谢谢各位的参与!
    shuichangliu 后最的那个sql :
    select a.kCode,
           [kMoney]=sum(a.kMoney*(case a.kType when 'no' then 1 else  a.kOnum end))
           into #tmpTab   from test a   group by a.kCode  order by kMoney desc
    select top 100 * from #tmpTab
    union all
    select '汇总',
           [knew_money]=sum(a.kMoney*(case a.kType when 'no' then 1 else  a.kOnum end)) from test a可以实现,即得到前5个最大的kmoney 如下:kcode    kmoney
    -----------------
    100001 41814.000000
    555666 17576.320000
    10300788 16340.000000
    1030190 15662.000000
    1030123 15220.000000但涉及到写入临时表,可不可以有更简便的方法呀!?