日期:2014-05-17  浏览次数:20523 次

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

SQL code

字段
ID UID GIF
1   3  DK
2   2  DF
3   4  RT
4   6  DK
5   4  DF
6   8  JF
7   2  RT
8   9  GH
9   12 DK
10  23 DK
11  2  RT
12  90 GR
13  43 SD

输出效果:

GIF COUNT
DK    4
RT    3
DF    2
JF    1
GH    1
 



------解决方案--------------------
试一下这个能不能合适你的要求
SQL code
select top 5 GIF,count(1) AS [COUNT]
from test
where GIF in (xx)
group by GIF
ORDER BY COUNT(1) DESC

------解决方案--------------------
SQL code

--> 测试数据: @T
declare @T table (ID varchar(2),UID varchar(3),GIF varchar(3))
insert into @T
select '1','3','DK' union all
select '2','2','DF' union all
select '3','4','RT' union all
select '4','6','DK' union all
select '5','4','DF' union all
select '6','8','JF' union all
select '7','2','RT' union all
select '8','9','GH' union all
select '9','12','DK' union all
select '10','23','DK' union all
select '11','2','RT' union all
select '12','90','GR' union all
select '13','43','SD'

select 
    GIF,count(*) as [COUNT]
from @T group by GIF order by 2 desc
/*
GIF  COUNT
---- -----------
DK   4
RT   3
DF   2
SD   1
GH   1
GR   1
JF   1

(7 row(s) affected)
*/