Average salary for men and women, but separated

SasNarSak :

I have to: "Write a SQL code showing for each state that has more than one male employee and one female employee the number of male employees and the number of female employees, the average salary for men and the average salary for women."

I know that it should be grouped by gender as well, but then it doesn't display anything? What should I do? The problem is that now I'm getting an average salary of both genders combined. I think that the rest of my code is fine.

SELECT gender, state, AVG(salary),
COUNT(CASE WHEN gender='M' THEN 1  END) AS men,
COUNT(CASE WHEN gender='F' THEN 1  END) AS women
FROM employee
GROUP BY state
HAVING men>1 && women>1
ORDER BY state, gender DESC;
juergen d :
SELECT state, 
       sum(gender='M') as men_count,
       sum(gender='F') as women_count,
       AVG(case when gender='M' then salary end) as men_avg_sal,
       AVG(case when gender='F' then salary end) as women_avg_sal
FROM employee
GROUP BY state
HAVING sum(gender='M') > 1
   AND sum(gender='F') > 1
ORDER BY state

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=394750&siteId=1