sql查询问题,在线等 急!!!!!!!!

huoyutu 2008-07-16 08:54:54
我现在有一张表 如
so_id so_name so_type
1 国家发改委 1
2 价格监测中心 2
3 广州电台 3
4 广州商业电台 3

我需要的结果是

so_id so_name so_type count
1 国家发改委 1 1
2 价格监测中心 2 1
3 广州电台 3 2
4 广州商业电台 3 2

请问一条SQL语句怎么实现
...全文
93 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dsp_zerg 2008-07-16
  • 打赏
  • 举报
回复

create table test(so_id int,so_name varchar(20),so_type int)
insert test(so_id,so_name,so_type) values(1,'国家发改委',1)
insert test(so_id,so_name,so_type) values(2,'价格监测中心',2)
insert test(so_id,so_name,so_type) values(3,'广州电台',3)
insert test(so_id,so_name,so_type) values(4,'广州商业电台',3)

select a.so_id,a.so_name,a.so_type,b.count from test a
left join
(select so_type,count(so_type)as count from test group by so_type) b
on a.so_type=b.so_type
-狙击手- 2008-07-16
  • 打赏
  • 举报
回复
。。
huoyutu 2008-07-16
  • 打赏
  • 举报
回复
谢谢各位大虾
结贴了
网上牛人确实多
liangCK 2008-07-16
  • 打赏
  • 举报
回复
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (so_id int,so_name nvarchar(12),so_type int)
insert into #T
select 1,'国家发改委',1 union all
select 2,'价格监测中心',2 union all
select 3,'广州电台',3 union all
select 4,'广州商业电台',3

select so_id,so_name,so_type,
(select count(*) from #T where so_type=t.so_type) so_count
from #T t

/*
so_id so_name so_type so_count
----------- ------------ ----------- -----------
1 国家发改委 1 1
2 价格监测中心 2 1
3 广州电台 3 2
4 广州商业电台 3 2

(4 行受影响)
*/
wzy_love_sly 2008-07-16
  • 打赏
  • 举报
回复
so_id so_name so_type
1 国家发改委 1
2 价格监测中心 2
3 广州电台 3
4 广州商业电台 3

select soid,so_name,so_type,
[count]=(select count(1) from tb where so_type=t.so_type)
from tbname t
huoyutu 2008-07-16
  • 打赏
  • 举报
回复
不好意思,忘记说明了

count 是根据so_type字段统计出来的
liangCK 2008-07-16
  • 打赏
  • 举报
回复
没人想去猜.
qjlsharp 2008-07-16
  • 打赏
  • 举报
回复
count字段是指的什么,怎么来的?
-狙击手- 2008-07-16
  • 打赏
  • 举报
回复
count是相似的合计 ?

34,838

社区成员

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

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