Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi ,
i am facing problem to writing sql query

staff table

staff_id staff_name dept_id gender salaries
1 A 1 M 10
2 B 1 M 20
3 c 2 F 30
4 D 2 F 40
5 E 1 F 50

Department table

dept_id dept_name
1 Radiology
2 Cardiology

O/P required

dept_name sum male salary sum female salary
Radiology 30 50
Cardiology _ 70

please help me.

thanks in advance...
Posted

SQL
select R.SUM(Salaries ),R.gender ,R.dept_name(select D.dept_name,S.Salaries,S.gender from   staff S left outer join  
Department D on (D.ID=s.dept_id ) where S.gender='M'  )as R group by R.dept_name
 
Share this answer
 
SQL
SELECT dept_name, [sum male salary], [sum female salary]
FROM Department D
LEFT JOIN
(
    SELECT SUM(Salaries) AS [sum male salary],dept_id  FROM Staff
    GROUP BY dept_id, gender
    HAVING gender = 'M'
) M ON D.dept_id = M.dept_id
LEFT JOIN
(
    SELECT SUM(Salaries) AS [sum female salary],dept_id  FROM Staff
    GROUP BY dept_id, gender
    HAVING gender = 'F'
) F ON D.dept_id = F.dept_id
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900