当前位置: 代码迷 >> Sql Server >> MSSQL2000统计某列中多个指定字符串的总和,并显示最多的5记录,从大到小排序
  详细解决方案

MSSQL2000统计某列中多个指定字符串的总和,并显示最多的5记录,从大到小排序

热度:73   发布时间:2016-04-27 11:34:34.0
MSSQL2000统计某列中多个指定字符串的总数,并显示最多的5记录,从大到小排序
表 test

SQL code
字段ID UID GIF1   3  DK2   2  DF3   4  RT4   6  DK5   4  DF6   8  JF7   2  RT8   9  GH9   12 DK10  23 DK11  2  RT12  90 GR13  43 SD输出效果:GIF COUNTDK    4RT    3DF    2JF    1GH    1 


------解决方案--------------------
试一下这个能不能合适你的要求
SQL code
select top 5 GIF,count(1) AS [COUNT]from testwhere GIF in (xx)group by GIFORDER BY COUNT(1) DESC
------解决方案--------------------
SQL code
--> 测试数据: @Tdeclare @T table (ID varchar(2),UID varchar(3),GIF varchar(3))insert into @Tselect '1','3','DK' union allselect '2','2','DF' union allselect '3','4','RT' union allselect '4','6','DK' union allselect '5','4','DF' union allselect '6','8','JF' union allselect '7','2','RT' union allselect '8','9','GH' union allselect '9','12','DK' union allselect '10','23','DK' union allselect '11','2','RT' union allselect '12','90','GR' union allselect '13','43','SD'select     GIF,count(*) as [COUNT]from @T group by GIF order by 2 desc/*GIF  COUNT---- -----------DK   4RT   3DF   2SD   1GH   1GR   1JF   1(7 row(s) affected)*/