CREATE OR REPLACE VIEW food600.spol_plt
AS 
SELECT b.uzaverka,
    b.u_cis,
    b.prachy_pol,
    b.prachy_plt,
    b.round50,
        CASE
            WHEN (b.prachy_pol + b.round50) <> 0::numeric THEN round((b.prachy_plt / (b.prachy_pol + b.round50))::numeric(16,9), 9)
            ELSE 1::numeric
        END AS slev_koef,
    b.c_data
   FROM ( SELECT a.uzaverka,
            a.u_cis,
            sum(a.prachy_pol) AS prachy_pol,
            sum(a.prachy_plt) AS prachy_plt,
            sum(a.round50) AS round50,
            a.c_data
           FROM ( SELECT sk_pol.c_data,
                    sk_pol.uzaverka,
                    sk_pol.u_cis,
                    sum(sk_pol.prachy) AS prachy_pol,
                    0 AS prachy_plt,
                    0 AS round50
                   FROM food600.sk_pol
                  WHERE sk_pol.k_odpoctu <> ''::bpchar  AND sk_pol.kompez = 0
                  GROUP BY sk_pol.c_data, sk_pol.uzaverka, sk_pol.u_cis
                UNION ALL
                 SELECT sk_plt.c_data,
                    sk_plt.uzaverka,
                    sk_plt.u_cis,
                    0 AS prachy_pol,
                    sum(sk_plt.cena_pl) AS prachy_plt,
                    0 AS round50
                   FROM food600.sk_plt
                  WHERE sk_plt.kompez = 0
                  GROUP BY sk_plt.c_data, sk_plt.uzaverka, sk_plt.u_cis
                UNION ALL
                 SELECT sk_fin.c_data,
                    sk_fin.uzaverka,
                    sk_fin.u_cis,
                    0 AS prachy_pol,
                    0 AS prachy_plt,
                    sum(sk_fin.round50) AS round50
                   FROM food600.sk_fin
                  GROUP BY sk_fin.c_data, sk_fin.uzaverka, sk_fin.u_cis) a
          GROUP BY a.c_data, a.uzaverka, a.u_cis) b;