CREATE OR REPLACE VIEW food600.spol_plt_a
 AS
 SELECT b.uzaverka,
    b.u_cis,
    b.prachy_pol,
    b.prachy_plt,
    b.round50,
        CASE
            WHEN (b.prachy_pol + b.round50) <> 0 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_a.c_data,
                    sk_pol_a.uzaverka,
                    sk_pol_a.u_cis,
                    sum(sk_pol_a.prachy) AS prachy_pol,
                    0 AS prachy_plt,
                    0 AS round50
                   FROM food600.sk_pol_a
                  WHERE sk_pol_a.k_odpoctu <> ''::bpchar AND sk_pol_a.kompez = 0
                  GROUP BY sk_pol_a.c_data, sk_pol_a.uzaverka, sk_pol_a.u_cis
                UNION ALL
                 SELECT sk_plt_a.c_data,
                    sk_plt_a.uzaverka,
                    sk_plt_a.u_cis,
                    0 AS prachy_pol,
                    sum(sk_plt_a.cena_pl) AS prachy_plt,
                    0 AS round50
                   FROM food600.sk_plt_a
                  WHERE sk_plt_a.kompez = 0
                  GROUP BY sk_plt_a.c_data, sk_plt_a.uzaverka, sk_plt_a.u_cis
                UNION ALL
                 SELECT sk_fin_a.c_data,
                    sk_fin_a.uzaverka,
                    sk_fin_a.u_cis,
                    0 AS prachy_pol,
                    0 AS prachy_plt,
                    sum(sk_fin_a.round50) AS round50
                   FROM food600.sk_fin_a
                  GROUP BY sk_fin_a.c_data, sk_fin_a.uzaverka, sk_fin_a.u_cis) a
          GROUP BY a.c_data, a.uzaverka, a.u_cis) b;

