遇到了不会写的sql,没有思路

图片说明

 select max(year) as '年份',
       (select count(*) from 表 b where b.sex='男' and b.company='c1' and a.year=b.year) as 'c1男' ,
       (select count(*) from 表 c where c.sex='女' and c.company='c1' and a.year=c.year) as 'c1女' ,
       (select count(*) from 表 d where d.sex='男' and d.company='c2' and a.year=d.year) as 'c2男' ,
       (select count(*) from 表 e where e.sex='女' and e.company='c2' and a.year=e.year) as 'c2女' ,
       (select count(*) from 表 f where a.year=f.year) as '合计'

from 表 a group by a.year union all
select '合计', 
       (select count(*) from 表  where sex='男' and company='c1') as 'c1男' ,
       (select count(*) from 表  where sex='女' and company='c1') as 'c1女' ,
       (select count(*) from 表  where sex='男' and company='c2') as 'c2男' ,
       (select count(*) from 表  where sex='女' and company='c2') as 'c2女' ,
       (select count(*) from 表  ) as '合计'
 from 表 group by '合计'

select 年份,sum((case when 公司=‘c1’ and 性别=‘男’ then 1 else
0 end ))as c1_男,
sum((case when 公司=‘c1’ and 性别=‘女’ then 1 else
0 end ))as c1_女,
sum((case when 公司=‘c2’ and 性别=‘男’ then 1 else
0 end ))as c1_男,

sum((case when 公司=‘c2’ and 性别=‘女’ then 1 else
0 end ))as c1_女
from 表1 group by 年份

select 年份,sum((case when 公司=‘c1’ and 性别=‘男’ then 1 else
0 end ))as c1_男,
sum((case when 公司=‘c1’ and 性别=‘女’ then 1 else
0 end ))as c1_女,
sum((case when 公司=‘c2’ and 性别=‘男’ then 1 else
0 end ))as c2_男,

sum((case when 公司=‘c2’ and 性别=‘女’ then 1 else
0 end ))as c2_女 ,
sum(decode(姓名,null,0,1)) as 合计
from 表1 group by 年份

别人说了 公司是动态的。性别用我觉得用count+case

select
q4 AS '年份',
count(case when q2= 31 ANDq3=0 then 0 end)c1男,
count(case when q2= 31 ANDq3=1 then 1 end)c1女,
count(case when q2= 32 ANDq3=0 then 0 end)c2男,
count(case when q2= 32 ANDq3=1 then 1 end)c2女,
COUNT(*) AS '合计'
from user
GROUP BY q4union all
select '合计',
(select count(case when q2= 31 ANDq3=0 then 0 end)c1男),
(select count(case when q2= 31 ANDq3=1 then 1 end)c1女),
(select count(case when q2= 32 ANDq3=0 then 0 end)c2男),
(select count(case when q2= 32 ANDq3=1 then 1 end)c2女),
(select count(*) from user ) as '合计'
from user group by '合计'
图片说明