I want to get Count and Average values from two different tables as one final output.
I use case statements to get the records fusioned, but which doesn’t work.
The team Pole_B is present in both tables and i want to get the values of both tables in the same line.
What i get actually wrong is :
team_a team_b pts_a pts_b avg_c pts_d pts_e avg_f
Pole_A Pole_B 12 12 3.7 12 12 3.0
Pole_A Pole_C 12 12 3.7 12 12 3.0
Pole_A Pole_D 12 12 3.7 12 12 3.8
Pole_B Pole_B 16 16 2.8 16 16 3.0
Pole_B Pole_C 16 16 2.8 16 16 3.0
Pole_B Pole_D 16 16 2.8 16 16 3.8
What i want to get is:
team_ab pts_a pts_b avg_c pts_d pts_e avg_f
Pole_A 3 3 3,7 0 0 0
Pole_B 4 4 2,8 4 4 3,0
Pole_C 0 0 0 4 4 3,0
Pole_D 0 0 0 4 4 3,8
Here is my query :
SELECT a.team_a, b.team_b,
COUNT(CASE WHEN a.pts_a !=99 THEN 1 ELSE 0 END) AS pts_a,
COUNT(CASE WHEN a.pts_b !=99 THEN 1 ELSE 0 END) AS pts_b,
ROUND(AVG(CASE WHEN a.avg_c IN(1,2,3,4,5) THEN avg_c END),1) AS avg_c,
COUNT(CASE WHEN b.pts_d !=99 THEN 1 ELSE 0 END) AS pts_d,
COUNT(CASE WHEN b.pts_e !=99 THEN 1 ELSE 0 END) AS pts_e,
ROUND(AVG(CASE WHEN b.avg_f IN(1,2,3,4,5) THEN avg_f END),1) AS avg_f
FROM tbla a
LEFT JOIN tblb b ON a.week_a = b.week_b
GROUP BY team_a, team_b
ORDER BY team_a ASC;
I have create a fiddle if somebody can take a look inside.
Here is my Sql Fiddle
Thanks in advance