Why is GROUP BY clause missing rows
UNION excludes duplicate rows.
You want to use UNION ALL in this example:
 SELECT id, SUM(num) as sum
 FROM (
     SELECT 1 AS id, 2 AS num UNION ALL
     SELECT 1, 2 /*!*/) AS a
 GROUP BY id

Results:
id          sum
----------- -----------
1           4