select t3.companyId,t3.companyName as companyName , t1.coachNum, t2.guideNum, t3.playerNum , IFNULL(t1.coachNum,0) + IFNULL(t2.guideNum,0) + IFNULL(t3.playerNum,0) as sum from
(select t.companyId, t.companyName,count(t.id) as playerNum from
(SELECT
tcc.`name` as companyName,
tcc.id,
(SELECT count(tcca.id) FROM t_compete_coach tcca WHERE tcc.id = tcca.company_id and identity = 1 and rec_status = 0) as coachNum,
(SELECT count(tcca.id) FROM t_compete_coach tcca WHERE tcc.id = tcca.company_id and identity = 0 and rec_status = 0) as guideNum,
((SELECT COUNT(tcpp.id) FROM t_compete_project_player tcpp LEFT JOIN t_compete_player tcp on tcp.id = tcpp.player_id WHERE tcp.company_id = tcc.id and tcp.rec_status = 0 and tcpp.rec_status = 0 and tcp.rec_status = 0) +
(SELECT COUNT(tctm.id) FROM t_compete_team_member tctm LEFT JOIN t_compete_team tct on tctm.compete_team_id = tct.id WHERE tct.company_id = tcc.id and tct.rec_status = 0 and tctm.rec_status = 0 ))as playerNum
DISTINCT(tcp.id) as id, tcc.id as companyId,tcc.`name` as companyName
FROM
t_compete_company tcc
left join
t_compete_player tcp on tcc.id = tcp.company_id
LEFT JOIN t_compete_project_player tcpp ON tcp.id = tcpp.player_id
WHERE
tcc.type = 0
AND tcp.company_id = tcc.id
AND tcc.rec_status = 0
AND tcp.rec_status = 0
AND tcpp.rec_status = 0
UNION
SELECT
DISTINCT(tcp.id) as id,tcc.id as companyId,tcc.`name` as companyName
FROM
t_compete_company tcc
LEFT JOIN t_compete_team tct ON tcc.id = tct.company_id
LEFT JOIN t_compete_team_member tctm on tct.id = tctm.compete_team_id
LEFT JOIN t_compete_player tcp ON tctm.player_id = tcp.id
WHERE
tcc.rec_status =0
and tcc.compete_time_id = #{competeTimeId}
) c
WHERE c.playerNum <> 0
tcc.type = 0
AND tct.rec_status = 0
AND tctm.rec_status = 0
And tcc.rec_status = 0
AND tcp.rec_status = 0) t
group by t.companyId
having count(t.id) > 0
) t3
left join
(SELECT tcc.id as companyId,tcc.`name` as companyName ,count(tcca.id) as coachNum FROM t_compete_company tcc left join (select * from t_compete_coach where rec_status = 0) tcca on tcc.id = tcca.company_id where tcc.type = 0 AND identity = 1 and tcc.rec_status = 0 GROUP BY tcc.id) t1 on t1.companyId = t3.companyId
left join
(SELECT tcc.id as companyId,tcc.`name` as companyName , count(tcca.id) as guideNum FROM t_compete_company tcc left join (select * from t_compete_coach where rec_status = 0) tcca on tcc.id = tcca.company_id where tcc.type = 0 AND identity = 0 and tcc.rec_status = 0 GROUP BY tcc.id) t2 on t2.companyId = t3.companyId
select s.*, c.name as companyName, GROUP_CONCAT(p.name) as peopleName, (ifnull(s.referee1,0)+ifnull(s.referee2,0)+ifnull(s.referee3,0))/3 as avg1To3,
(ifnull(s.referee4,0)+ifnull(s.referee5,0)+ifnull(s.referee6,0))/3 as avg4To6,
(ifnull(s.referee1,0)+ifnull(s.referee2,0)+ifnull(s.referee3,0))/3 + (ifnull(s.referee4,0)+ifnull(s.referee5,0)+ifnull(s.referee6,0))/3 - s.referee0 as result,
case o.waiver when 1 then '弃权' when 2 then '取消比赛资格' end as remark
from
(select site_order_id, max(if(chief_judgment = 0, score, null)) as referee0, max(if(chief_judgment = 1, score, null)) as referee1,
max(if(chief_judgment = 2, score, null)) as referee2, max(if(chief_judgment = 3, score, null)) as referee3,
max(if(chief_judgment = 4, score, null)) as referee4, max(if(chief_judgment = 5, score, null)) as referee5,
max(if(chief_judgment = 6, score, null)) as referee6, max(if(chief_judgment = 7, score, null)) as referee7
from
(select s.site_order_id, j.chief_judgment, sum(s.score) as score from t_compete_variety_score s, t_compete_judgment j where s.judgment_id = j.id and s.project_id = #{projectId} and s.site_order_id = #{siteOrderId} and s.rec_status = 0 and j.rec_status = 0 group by s.site_order_id,j.chief_judgment) t
group by t.site_order_id) s,
t_compete_start_order o,
t_compete_team t,
t_compete_team_member m,
t_compete_player p,
t_compete_company c
where s.site_order_id = o.id and o.player_id = t.id and t.id = m.compete_team_id and m.player_id = p.id and p.company_id = c.id
and o.rec_status = 0 and t.rec_status = 0 and m.rec_status = 0 and p.rec_status = 0 and c.rec_status = 0