求sql语句

songweida8076 2011-02-25 04:54:23
表A
id name 工资 部门
1 aa 2000 开发
2 bb 3000 销售
3 cc 2000 开发
4 dd 5000 销售
5 ee 8000 技术
6 ff 3500 技术



实现成
部门 2000~4000 4001~6000 6001~8000
开发 2人 0人 0人
销售 1人 1人 0人
技术 0人 0人 2人
...全文
77 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
songweida8076 2011-02-25
  • 打赏
  • 举报
回复
with A as(
select 1 id, 'aa' name, 2000 工资, '开发' 部门 from dual
union all
select 2, 'bb', 3000, '销售' from dual
union all
select 3, 'cc', 2000, '开发' from dual

with a 什么意思 from dual dual在哪弄的
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 songweida8076 的回复:]
都不好用,我家里就sql server,实现不了,报错。
[/Quote]

sql server 跑oracle板块来问...
songweida8076 2011-02-25
  • 打赏
  • 举报
回复
都不好用,我家里就sql server,实现不了,报错。
liuyyuns 2011-02-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gelyon 的回复:]
SQL code


with A as(
select 1 id, 'aa' name, 2000 工资, '开发' 部门 from dual
union all
select 2, 'bb', 3000, '销售' from dual
union all
select 3, 'cc', 2000, '开发' from dual
union all
select 4, 'dd……
[/Quote]
写的很好
gelyon 2011-02-25
  • 打赏
  • 举报
回复


with A as(
select 1 id, 'aa' name, 2000 工资, '开发' 部门 from dual
union all
select 2, 'bb', 3000, '销售' from dual
union all
select 3, 'cc', 2000, '开发' from dual
union all
select 4, 'dd', 5000, '销售' from dual
union all
select 5, 'ee', 8000, '技术' from dual
union all
select 6, 'ff', 3500, '技术' from dual
)
select 部门,
count(case when 2000<=工资 and 工资<4000 then 1 else null end) "2000~4000",
count(case when 4001<=工资 and 工资<6000 then 1 else null end) "4001~6000",
count(case when 6001<=工资 and 工资<8000 then 1 else null end) "6001~8000"
from a
group by 部门

部门 2000~4000 4001~6000 6001~8000
--------------------------------------------
开发 2 0 0
技术 1 0 0
销售 1 1 0
gelyon 2011-02-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 songweida8076 的回复:]
帮我解释下可以吗,第一行 2000~4000 也能显示出来?
[/Quote]
这样的别名要用双引号括起来: "2000~4000"
songweida8076 2011-02-25
  • 打赏
  • 举报
回复
帮我解释下可以吗,第一行 2000~4000 也能显示出来?
  • 打赏
  • 举报
回复

select 部门,
count(case when 2000<=工资<4000 then 1 else null end) 2000~4000,
count(case when 4001<=工资<6000 then 1 else null end) 4001~6000,
count(case when 6001<=工资<8000 then 1 else null end) 6001~8000
from tb
group by 部门

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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