高分请教一个SQL查询语句

boyz 2005-12-19 04:27:48
表info,有字段: name,branch,age
表中有数据:
王一,营业部,22
赵二,营业部,18
陈平,营业部,29
李红,科技部,32
周胜,会计部,23


要求分别按branch统计出age在20岁以上的人数
查询结果如下:

营业部 科技部 会计部
2 1 1


查询语句怎么写?

...全文
464 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
boyz 2005-12-26
  • 打赏
  • 举报
回复
谢谢大家,真的很感谢,散分
建哥聊IT 2005-12-23
  • 打赏
  • 举报
回复
收藏!
gaojie001 2005-12-20
  • 打赏
  • 举报
回复
试过了,没问题
gaojie001 2005-12-20
  • 打赏
  • 举报
回复
select franch,count(age) from aa where age>20 group by franch
玩不够 2005-12-20
  • 打赏
  • 举报
回复
作了小改动更适合实际呀!

CREATE TABLE #info
(
name nvarchar (10) NULL ,
branch nvarchar (10) NULL ,
age tinyint NULL
)
insert into #info values
('王一','营业部','22')
insert into #info values
('赵二','营业部','18')
insert into #info values
('陈平','营业部','29')
insert into #info values
('李红','科技部','32')
insert into #info values
('周胜','会计部','23')

create table #ainfo
(
Nu tinyint,
ageset1 tinyint,
ageset2 tinyint
)
insert into #ainfo values
(1,1,20)
insert into #ainfo values
(2,21,30)
insert into #ainfo values
(3,31,99)
GO

select
b.Nu,'('+str(b.ageset1,2)+'到'+str(b.ageset2,2)+'之间的人数)',
sum(case when branch='营业部' then 1 else 0 end) 营业部,
sum(case when branch='科技部' then 1 else 0 end) 科技部,
sum(case when branch='会计部' then 1 else 0 end) 会计部
from #info a
inner join #ainfo b ON a.age between ageset1 and ageset2
group by b.Nu,b.ageset1,b.ageset2

drop table #info
drop table #ainfo

Nu 营业部 科技部 会计部
---- ------------------ ----------- ----------- -----------
1 ( 1到20之间的人数) 1 0 0
2 (21到30之间的人数) 2 0 1
3 (31到99之间的人数) 0 1 0

boyz 2005-12-20
  • 打赏
  • 举报
回复
谢谢大家,可以用。
————————————————————————

再加一个需求 在之前的基础上,增加一个表 ageinfo
字段如下
no 编号
ageset 年龄段

里面有记录
1 20
2 30

————————————————————————
要求结合表ageinfo 统计出不同部门,不同年龄段的人数

查询结果如下


营业部 科技部 会计部
----------- ----------- -----------
(20岁以下人数) 1 0 0
(30岁以下人数) 2 1 1


玩不够 2005-12-20
  • 打赏
  • 举报
回复
还是 mislrb(aben) 的方法好动态的
hpym365 2005-12-19
  • 打赏
  • 举报
回复
提醒一下各位啊如果部门有1000个 就不能这么写了吧??
有没有别的方法?
select
营业部=sum(case when Adepartment='营业部' and age>20 then 1 else 0 end),
科技部=sum(case when Adepartment='科技部' and age>20 then 1 else 0 end),
会计部=sum(case when Adepartment='会计部' and age>20 then 1 else 0 end)
from a
玩不够 2005-12-19
  • 打赏
  • 举报
回复
select
*
from dbo.info

select
sum(case when branch='营业部' and age>20 then 1 else 0 end) 营业部,
sum(case when branch='科技部' and age>20 then 1 else 0 end) 科技部,
sum(case when branch='会计部' and age>20 then 1 else 0 end) 会计部
from dbo.info

结果:
name branch age
---------- ---------- -----------
王一 营业部 22
赵二 营业部 18
陈平 营业部 29
李红 科技部 32
周胜 会计部 23

(所影响的行数为 5 行)

营业部 科技部 会计部
----------- ----------- -----------
2 1 1
hpym365 2005-12-19
  • 打赏
  • 举报
回复
mark
boyz 2005-12-19
  • 打赏
  • 举报
回复
select
营业部=sum(case when branch='营业部' and age>20 then 1 else 0 end),
科技部=sum(case when branch='科技部' and age>20 then 1 else 0 end),
会计部=sum(case when branch='会计部' and age>20 then 1 else 0 end)
from info


不行啊 我这里统计出来都是空的 而且还不能用自定义的列
数据库是informix
wangdehao 2005-12-19
  • 打赏
  • 举报
回复
select
营业部=sum(case when branch='营业部' and age>20 then 1 else 0 end),
科技部=sum(case when branch='科技部' and age>20 then 1 else 0 end),
会计部=sum(case when branch='会计部' and age>20 then 1 else 0 end)
from info

boyz 2005-12-19
  • 打赏
  • 举报
回复
执行
select branch,count(age)
from info
group by branch
having age>20
order by branch

总是报错啊,提示
HAVING can only have expressions with aggregates or columns in GROUP BY clause
boyz 2005-12-19
  • 打赏
  • 举报
回复
可以不用存储过程吗, 实现行列转换的


会计部 科技部 营业部
----------- ----------- -----------
1 1 2

这样的效果
WeekZero 2005-12-19
  • 打赏
  • 举报
回复
select
营业部=sum(case when Adepartment='营业部' and age>20 then 1 else 0 end),
科技部=sum(case when Adepartment='科技部' and age>20 then 1 else 0 end),
会计部=sum(case when Adepartment='会计部' and age>20 then 1 else 0 end)
from a

这个如果行,真是写的太好了
lxw99 2005-12-19
  • 打赏
  • 举报
回复
select [会计部]=isnull(sum(case Adepartment when '会计部' then 1 end),0),[科技部]=isnull(sum(case Adepartment when '科技部' then 1 end),0),[营业部]=isnull(sum(case Adepartment when '营业部' then 1 end),0) from a where age>20

SQL Server 啊 努力 奋斗-----------------------------------------------------

lxw99 2005-12-19
  • 打赏
  • 举报
回复
drop table a
create table a(Aname varchar(10),Adepartment varchar(10),age int )
insert into a select '王一','营业部',22 union all
select '赵二','营业部',18 union all
select '陈平','营业部',29 union all
select '周胜','会计部',23 union all
select '李红','科技部',32

select
营业部=sum(case when Adepartment='营业部' and age>20 then 1 else 0 end),
科技部=sum(case when Adepartment='科技部' and age>20 then 1 else 0 end),
会计部=sum(case when Adepartment='会计部' and age>20 then 1 else 0 end)
from a


SQL Server 啊 努力 奋斗-----------------------------------------------------

ASPGuy 2005-12-19
  • 打赏
  • 举报
回复
select branch,count(age)
from info
group by branch
having age>20
order by branch
mislrb 2005-12-19
  • 打赏
  • 举报
回复
create table info(name varchar(10),branch varchar(10),age int)
insert info
select '王一','营业部',22 union all
select '赵二','营业部',18 union all
select '陈平','营业部',29 union all
select '李红','科技部',32 union all
select '周胜','会计部',23

declare @s varchar(8000)
set @s=''
select @s=@s+',['+branch+']=isnull(sum(case branch when '''+ branch + ''' then 1 end),0)' from info group by branch
select @s='select ' +stuff(@s,1,1,'')+' from info where age>20'

exec(@s)

drop table info

/*
会计部 科技部 营业部
----------- ----------- -----------
1 1 2

警告: 聚合或其它 SET 操作消除了空值。
*/
zlp321002 2005-12-19
  • 打赏
  • 举报
回复
select
营业部=sum(case when branch='营业部' and age>20 then 1 else 0 end),
科技部=sum(case when branch='科技部' and age>20 then 1 else 0 end),
会计部=sum(case when branch='会计部' and age>20 then 1 else 0 end)
from

加载更多回复(4)

34,838

社区成员

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

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