一个简单的数据库问题
在adventure works数据库中,有一个humanresources.employee资料表,这个资料表中记录员工个人资料,其中titile栏位表示职称,vacationhours栏位表示可以休假的时数,现在想知道哪一种职称休假时数 "总和 "最多
下面是我的写法,,请问有需要修改的地方或者有其他写法呢?!
select top 1 Title, sum(VacationHours) as TotalHours
from HumanResources.Employee
group by Title
order by TotalHours desc
------解决方案--------------------应该就是这个样子了
------解决方案--------------------top 1应该去掉,有可能TotalHours相同
------解决方案--------------------好象就這一種方法吧!要不然你用:
Select Title, sum(VacationHours) as TotalHours Into A From HumanResources.Employee Group By Title
Select Title,TotalHours From A Where TotalHours =(Select Max(TotalHours) From A)
Drop Table A
不過這種方法看起來很笨的!個人建議不提倡!還是樓主那個比較好!
------解决方案--------------------select top 1 Title, sum(VacationHours) as TotalHours
from HumanResources.Employee
group by Title
order by TotalHours desc
也可以写成:
select max(totalhours) from (
select Title, sum(VacationHours) as TotalHours
from HumanResources.Employee
group by Title) aa
------解决方案--------------------LZ写的很好了~
还有很多方法可以实现,我认为你的已经8错 了