日期:2014-05-17  浏览次数:20540 次

SQL 具体的问题
根据部门表(dept)和薪资表(salary),查询输出部门的薪资总数超过5000的部门的名称和该部门薪资总数:
depid dename
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

------其他解决方案--------------------
try
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

------其他解决方案--------------------
该回复于2012-11-10 16:11:32被管理员删除