有个表名cheliang有个列名shangjia 表内容显示如下
shangjia
上海大众
上海大众
上海大众
上海大众
上海大众
上海大众
一汽大众
一汽大众
一汽大众
一汽大众
一汽大众
一汽大众
一汽大众
桑塔纳
桑塔纳
桑塔纳
奥迪上海大众数量是5个 一汽大众是7个 桑塔纳是3个 奥迪是1个
要按数量大小排列显示出来
一汽大众
上海大众
桑塔纳
奥迪
shangjia
上海大众
上海大众
上海大众
上海大众
上海大众
上海大众
一汽大众
一汽大众
一汽大众
一汽大众
一汽大众
一汽大众
一汽大众
桑塔纳
桑塔纳
桑塔纳
奥迪上海大众数量是5个 一汽大众是7个 桑塔纳是3个 奥迪是1个
要按数量大小排列显示出来
一汽大众
上海大众
桑塔纳
奥迪
??
-- 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(shangjia) as paixu from cheliang group by shangjia) a
order by paixu desc
我想再显示一列数量数据
显示结果
shangjia shuliang
一汽大众 7
上海大众 5
桑塔纳 3
奥迪 1
select shangjia,count(1) as shuliang
from tb_a group by shangjia
order by shuliang desc
--结果
shangjia shuliang
一汽大众 7
上海大众 6
桑塔纳 3
奥迪 1