日期:2014-05-18  浏览次数:20456 次

求一条 sql 语句 (小弟积分不足先道歉了)

我想查询出数据表中   被使用的最多的个物品编号   只要求出该物品使用次数就可以了  
最好是一条sql语句  
表如下:   表id     物品id     使用时间
                    1             p2                 null
                    2             p2                 null
                    3             p1                 null
                    4             p3                 null
                    5             p1                 null

------解决方案--------------------
select top 1 物品id ,count(*) as 使用次数
from tablename
group by 物品id
order by count(*) desc

------解决方案--------------------
select top 1 物品id,count(物品id)as 使用次数 from 表
group by 物品id
order by count(物品id) desc
------解决方案--------------------
select top 1 物品id,count(1) from [Table] group by 物品id order by 2
------解决方案--------------------
create table #t( ID1 int,ID2 varchar(2),stime varchar(8))
insert #t
select 1, 'p2 ', ' '
union all select 2, 'p2 ', ' '
union all select 3, 'p1 ', ' '
union all select 4, 'p3 ', ' '
union all select 5, 'p1 ', ' '
select * from #t

select top 1 ID2 from (select ID2,icount = count(ID2) from #t group by ID2)A order by icount desc


drop table #t
(影響 5 個資料列)

ID1 ID2 stime
----------- ---- --------
1 p2
2 p2
3 p1
4 p3
5 p1

(影響 5 個資料列)

ID2
----
p2

(影響 1 個資料列)