select address_sheng,sum(Shengsum) from (
select case when Shengsum<5 then address_sheng else '其他' as address_sheng,
Shengsum from (
select *,row_number() over(order by ShengSum desc) as rn from (
select SUBSTRING(address_sheng,1,2) address_sheng ,count(address_sheng) Shengsum
 from Kehu_Info 
WHERE hezuoZt='1' group by address_sheng) as a
) as b) as c

解决方案 »

  1.   

    SQL2000,没有排序函数,也没有CTE以方便引用同一个结果我觉得最好的处理方式,就是直接用这个结果,按次数排序,然后在程序上只显示前几条,其余几条遍历合计到一行去,那是程序强项,速度很快。
      

  2.   

    小調一下select address_sheng,sum(Shengsum) from (
    select case when Shengsum<5 then address_sheng else '其他' as address_sheng,
    Shengsum from (
    select *,row_number() over(order by ShengSum desc) as rn from (
    select SUBSTRING(address_sheng,1,2) address_sheng ,count(address_sheng) Shengsum
     from Kehu_Info 
    WHERE hezuoZt='1' group by address_sheng) as a
    ) as b) as c
    group by address_sheng
      

  3.   

    小調一下select address_sheng,sum(Shengsum) from (
    select case when Shengsum<5 then address_sheng else '其他' as address_sheng,
    Shengsum from (
    select *,row_number() over(order by ShengSum desc) as rn from (
    select SUBSTRING(address_sheng,1,2) address_sheng ,count(address_sheng) Shengsum
     from Kehu_Info 
    WHERE hezuoZt='1' group by address_sheng) as a
    ) as b) as c
    group by address_sheng
    服务器: 消息 156,级别 15,状态 1,行 2
    在关键字 'as' 附近有语法错误。
    服务器: 消息 195,级别 15,状态 1,行 4
    'row_number' 不是可以识别的 函数名。
    服务器: 消息 156,级别 15,状态 1,行 7
    在关键字 'as' 附近有语法错误。
      

  4.   


    前几条数据好获取可以用  top 5 ,可是后面的数据有多少条不要确定啊!怎么通过程序合计
      

  5.   

    select *,identity(int,1,1) as rn into #temp from (
    select SUBSTRING(address_sheng,1,2) address_sheng ,count(address_sheng) Shengsum
     from Kehu_Info 
    WHERE hezuoZt='1' group by address_sheng) as a order by Shengsum descselect address_sheng,sum(Shengsum) from (
    select case when Shengsum<5 then address_sheng else '其他' as address_sheng,
    Shengsum from #temp as b) as c
    group by address_sheng
    drop table #temp
      

  6.   


    前几条数据好获取可以用  top 5 ,可是后面的数据有多少条不要确定啊!怎么通过程序合计
    不知道你的程序代码是什么
    以C#为例,因为前端反正是要遍历整个结果集输出结果的,for或者foreach,直接显示前5行,判断到5行以上时,把数据叠加到一个变量上,这样就可以了
      

  7.   


    还是错误啊 ,红色位置错误!
    服务器: 消息 156,级别 15,状态 1,行 7
    在关键字 'as' 附近有语法错误。
    少了一個end,再試一下。select *,identity(int,1,1) as rn into #temp from (
    select SUBSTRING(address_sheng,1,2) address_sheng ,count(address_sheng) Shengsum
     from Kehu_Info 
    WHERE hezuoZt='1' group by address_sheng) as a order by Shengsum desc
     
    select address_sheng,sum(Shengsum) from (
    select case when Shengsum<5 then address_sheng else '其他'  end as address_sheng,
    Shengsum from #temp as b) as c
    group by address_sheng
    drop table #temp
      

  8.   


    还是错误啊 ,红色位置错误!
    服务器: 消息 156,级别 15,状态 1,行 7
    在关键字 'as' 附近有语法错误。
    少了一個end,再試一下。select *,identity(int,1,1) as rn into #temp from (
    select SUBSTRING(address_sheng,1,2) address_sheng ,count(address_sheng) Shengsum
     from Kehu_Info 
    WHERE hezuoZt='1' group by address_sheng) as a order by Shengsum desc
     
    select address_sheng,sum(Shengsum) from (
    select case when Shengsum<5 then address_sheng else '其他'  end as address_sheng,
    Shengsum from #temp as b) as c
    group by address_sheng
    drop table #temp

    语句执行是没问题了 但是 不是我要的结果啊 
    能否加我QQ号 说呢 566971 select case when Shengsum<5 then address_sheng else '其他'  end as address_sheng,
    Shengsum from #temp as b
    你这句意思不对啊,我要的是Shengsum 最多的前5名而不是 >5 这种
      

  9.   


    还是错误啊 ,红色位置错误!
    服务器: 消息 156,级别 15,状态 1,行 7
    在关键字 'as' 附近有语法错误。
    少了一個end,再試一下。select *,identity(int,1,1) as rn into #temp from (
    select SUBSTRING(address_sheng,1,2) address_sheng ,count(address_sheng) Shengsum
     from Kehu_Info 
    WHERE hezuoZt='1' group by address_sheng) as a order by Shengsum desc
     
    select address_sheng,sum(Shengsum) from (
    select case when Shengsum<5 then address_sheng else '其他'  end as address_sheng,
    Shengsum from #temp as b) as c
    group by address_sheng
    drop table #temp

    语句执行是没问题了 但是 不是我要的结果啊 
    能否加我QQ号 说呢 566971 select case when Shengsum<5 then address_sheng else '其他'  end as address_sheng,
    Shengsum from #temp as b
    你这句意思不对啊,我要的是Shengsum 最多的前5名而不是 >5 这种
    rn<6 就是我要的意思了!
      

  10.   

    手誤,不好意思。再測試一下。select *,identity(int,1,1) as rn into #temp from (
    select SUBSTRING(address_sheng,1,2) address_sheng ,count(address_sheng) Shengsum
     from Kehu_Info 
    WHERE hezuoZt='1' group by address_sheng) as a order by Shengsum desc
      
    select address_sheng,sum(Shengsum) from (
    select case when rn<5 then address_sheng else '其他'  end as address_sheng,
    Shengsum from #temp as b) as c
    group by address_sheng
    drop table #temp