34,873
社区成员
发帖
与我相关
我的任务
分享
select shangjia
from (select shangjia,count(shangjia) as paixu from cheliang group by shangjia) a
order by paixu desc
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-05-27 11:46:25
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([shangjia] varchar(8))
insert [tb]
select '上海大众' union all
select '上海大众' union all
select '上海大众' union all
select '上海大众' union all
select '上海大众' union all
select '上海大众' union all
select '一汽大众' union all
select '一汽大众' union all
select '一汽大众' union all
select '一汽大众' union all
select '一汽大众' union all
select '一汽大众' union all
select '一汽大众' union all
select '桑塔纳' union all
select '桑塔纳' union all
select '桑塔纳' union all
select '奥迪'
--------------开始查询--------------------------
select shangjia from (select shangjia,count(1) as num from tb group by shangjia)t order by num desc
----------------结果----------------------------
/* shangjia
--------
一汽大众
上海大众
桑塔纳
奥迪
(4 行受影响)
*/select shangjia from (select shangjia,count(*)as Total from cheliang group by shangjia ) T order by T.Total DescSELECT * FROM (SELECT SHANGJIA,COUNT(1) 'COU' FROM cheliang GROUP BY SHANGJIA) T ORDER BY COU DESC
select shangjia,count(1) as shuliang
from tb_a group by shangjia
order by shuliang desc
--结果
shangjia shuliang
一汽大众 7
上海大众 6
桑塔纳 3
奥迪 1