当前位置: 代码迷 >> Sql Server >> 笔试中的一道SQL题,感兴趣的来看看,该怎么处理
  详细解决方案

笔试中的一道SQL题,感兴趣的来看看,该怎么处理

热度:83   发布时间:2016-04-27 21:23:23.0
笔试中的一道SQL题,感兴趣的来看看
给出三张表     表名(字段)

Employee(Employee_id,Department_id,Department_name)

Department(Department_id,Department_name)

Salary(Employee_id,salary)

要求按   Department_name   来统计salary总和,并且按照Department_id的降序来排列显示?

我当时想半天还是没弄出来,请大家帮忙看看!!

------解决方案--------------------
select Department_id,Department_name,sum(salary) S from Employee A Inner Join Salary B On a.Employee_id=b.Employee_id Group by Department_id,Department_name order by Department_id desc
------解决方案--------------------
--try


select A.Department_name, salary=sum(B.salary)
from Employee A
inner join Salary B on A.Employee_id=B.Employee_id
group by A.Department_name
order by min(A.Department_id) desc
------解决方案--------------------
我感觉楼主的题目是不是写错了..Employee(Employee_id,Department_id,Employee_name)
------解决方案--------------------
Employee(Employee_id,Department_id,Employee_name)
Department(Department_id,Department_name)
Salary(Employee_id,salary)
-----------------------------------

select t3.department_name,t4.salary from
Department t3,
(select sum(t1.salary) salary,t2.department_id from salary t1,employee t2 where t1.employee_id = t2.employee_id group by t2.department_id) t4
where
t3.department_id = t4.department_id
order by
t3.department_id desc

------解决方案--------------------
Employee(Employee_id,Department_id,Employee_name)
从数据库设计规范来看,这样设计是没有意义的
------解决方案--------------------
Employee(Employee_id,Department_id,Department_name)
---
Department_name明显是冗余字段
------解决方案--------------------
给出三张表 表名(字段)

Employee(Employee_id,Department_id,Department_name)

Department(Department_id,Department_name)

Salary(Employee_id,salary)

要求按 Department_name 来统计salary总和,并且按照Department_id的降序来排列显示?

select m.Department_id,m.Department_name from Employee m
left join
(
select Department_name,sum(salary) as salary from
(
select a.Department_name,a.Employee_id,b.salary from
Employee a,Salary b
where a.Employee_id = b.Employee_id
) t
group by Department_name
) n
on m.Department_name = n.Department_name
order by Department_id desc
  相关解决方案