请各位大哥帮忙写个查询语句

jay378416 2010-05-27 11:43:55
有个表名cheliang有个列名shangjia 表内容显示如下
shangjia
上海大众
上海大众
上海大众
上海大众
上海大众
上海大众
一汽大众
一汽大众
一汽大众
一汽大众
一汽大众
一汽大众
一汽大众
桑塔纳
桑塔纳
桑塔纳
奥迪

上海大众数量是5个 一汽大众是7个 桑塔纳是3个 奥迪是1个
要按数量大小排列显示出来
一汽大众
上海大众
桑塔纳
奥迪
...全文
61 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
v1ctory1216 2010-05-27
  • 打赏
  • 举报
回复

select shangjia
from (select shangjia,count(shangjia) as paixu from cheliang group by shangjia) a
order by paixu desc
--小F-- 2010-05-27
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- 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 行受影响)
*/
jwdream2008 2010-05-27
  • 打赏
  • 举报
回复
select shangjia from (select shangjia,count(*)as Total from cheliang group by shangjia ) T order by T.Total Desc

??
guguda2008 2010-05-27
  • 打赏
  • 举报
回复
SELECT * FROM (SELECT SHANGJIA,COUNT(1) 'COU' FROM cheliang GROUP BY SHANGJIA) T ORDER BY COU DESC
jaydom 2010-05-27
  • 打赏
  • 举报
回复

select shangjia,count(1) as shuliang
from tb_a group by shangjia
order by shuliang desc
--结果
shangjia shuliang
一汽大众 7
上海大众 6
桑塔纳 3
奥迪 1
jay378416 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 guguda2008 的回复:]
SQL code
SELECT * FROM (SELECT SHANGJIA,COUNT(1) 'COU' FROM cheliang GROUP BY SHANGJIA) T ORDER BY COU DESC
[/Quote]

我想再显示一列数量数据
显示结果
shangjia shuliang
一汽大众 7
上海大众 5
桑塔纳 3
奥迪 1

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧