日期:2014-05-18 浏览次数:20437 次
declare @table table(id int) insert into @table select 12 union all select 11 union all select 11 union all select 12 union all select 12 union all select 12 select list.* from @table list LEFT JOIN ( select id,COUNT(1) cou from @table group by id ) T ON T.id = list.id order by T.cou DESC /* (6 行受影响) id ----------- 12 12 12 12 11 11 (6 行受影响) */
------解决方案--------------------
create table #table (id int) insert into #table select 12 union all select 11 union all select 11 union all select 12 union all select 12 union all select 12 select id,count(1) as c from #table group by id order by c desc /* 12 4 11 2 */
------解决方案--------------------
select id,COUNT(id) from @table group by id ORDER BY COUNT(id) DESC