SQL 具体的问题
根据部门表(dept)和薪资表(salary),查询输出部门的薪资总数超过5000的部门的名称和该部门薪资总数:
depiddename
1部门一
2部门二
3部门三
personid depid salary
100001 1 2000
200001 2 3000
200002 2 4000
[最优解释]
use tempdb
go
if OBJECT_ID('Tempdb..#dept') is not null
drop table #dept
create TABLE #dept
(depid int,dename varchar(6))
insert #dept
select 1,'部门一' union all
select 2,'部门二' union all
select 3,'部门三'
if OBJECT_ID('Tempdb..#salary') is not null
drop table #salary
create TABLE #salary
(personid varchar(6),depid int,salary decimal(10,2))
insert #salary
select '100001',1,2000 union all
select '200001',2,3000 union all
select '200002',2,4000
go
select b.dename 部门名称,SUM(a.salary) 薪资总数
from #salary a inner join #dept b on a.depid=b.depid
group by b.dename
having SUM(a.salary)>5000
select a.dename,sum(b.salary) from
dept a,salary b where a.depid=b.depid
group by a.dename
having sum(b.salary)>5000