use example;
SELECT CONCAT(a.id, CONCAT(".", e.Id)) AS id,

      CASE
        WHEN a.a = 'foo' AND a.g = 'bar'  THEN SUM(b2.c - b1.c)
        WHEN a.a = 'foo' AND a.g <> 'bar' THEN h1.a - h2.a
        ELSE 1.3579
      END AS a,

      CASE
        WHEN a.a = 'foo' THEN h1.b
        ELSE 1.4812
      END AS b,

      CASE
        WHEN a.a = 'foo' AND a.g =  'bar' THEN SUM((b2.b - b1.b)*1.0000)
        WHEN a.a = 'foo' AND a.g <> 'bar' THEN (h1.a * h1.b - h2.a * h2.b) * e.b
        ELSE 1.51015
      END AS c,

      CASE
        WHEN a.a = 'foo' AND a.g =  'bar' THEN 2.3456
        WHEN a.a = 'foo' AND a.g <> 'bar' THEN (h1.a * h1.b - h2.a * h2.b) * e.b * 1.0000
        ELSE 1.2345
      END AS d,

      CASE WHEN a.a = 'foo' THEN 1 ELSE 2 END AS e,

      h1.id AS this_should_be_a_bigint,
      a.f as what_it_should_be,

      COALESCE(GROUP_CONCAT(DISTINCT d.b ORDER BY d.b SEPARATOR ', '), '') AS f

    FROM p
    JOIN o      ON o.sid = p.id
    JOIN a      ON a.c   = o.id
    LEFT JOIN l ON a.b   = l.id
    LEFT JOIN c ON l.a   = c.Id
    LEFT JOIN m ON a.d   = m.Id
    LEFT JOIN h AS h1 ON h1.id = a.f
    LEFT JOIN h AS h2 ON h2.id = a.e
    LEFT JOIN e ON a.id = e.a
    LEFT JOIN w ON e.c  = w.Id
    LEFT JOIN t ON a.id = t.a
    LEFT JOIN w as w1 ON t.b = w1.b
    LEFT JOIN w as w2 ON t.c = w2.b
    LEFT JOIN b as b1 ON b1.a = w1.id
    LEFT JOIN b as b2 ON b2.a = w2.id
    LEFT JOIN d ON d.a = w.id
    GROUP BY a.id, e.Id \G
