1. 程式人生 > >oracle case when 查詢各個部門不同工資階段的人數

oracle case when 查詢各個部門不同工資階段的人數

select d.deptName,e.level1,e.level2,e.level3 from
(select deptId,count(case when wage < 5000 then 1 else null end ) level1,count(case when wage >=5000 and wage <15000 then 1 else null end ) level2,
count(case when wage > 15000 then 1 else null end ) level3 from t_employee  group by deptId) e 
left join 
t_dept d 
on
e.deptId = d.deptId

      最近一好友問我假設有兩個表。一個是employee表,記錄這員工的基本資訊包括工資、部門id等,另一張表是dept表,記錄這部門的相關資訊。如何通過這兩張表,來查詢出各個部門不同工資階段的人數呢?

        經過查詢相關資料,終於寫出這個sql語句,經除錯,能達到要求。本人小白,大佬不知道哪位大佬有效率更高更簡便的寫法呀。歡迎指點。