CREATE OR REPLACE VIEW food600.ssk__pol_dph1
 AS
 SELECT a.c_uzaverka,
    a.ucet,
    a.c_stredisk,
    a.c_karty,
        CASE
            WHEN b.prachy_sum <> 0::numeric THEN round((a.prachy * b.cena_pl / b.prachy_sum)::numeric(12,2), 2)
            ELSE a.prachy
        END AS prachy,
        CASE
            WHEN b.prachy_sum <> 0::numeric THEN round((a.prachy_puv * b.cena_pl / b.prachy_sum)::numeric(12,2), 2)
            ELSE a.prachy_puv
        END AS prachy_puv,
        CASE
            WHEN b.prachy_sum <> 0::numeric THEN round((a.dan * b.cena_pl / b.prachy_sum)::numeric(12,2), 2)
            ELSE a.dan
        END AS dan,
    a.dan_sazba,
        CASE
            WHEN b.prachy_sum <> 0::numeric THEN round((a.mnozstvi * b.cena_pl / b.prachy_sum)::numeric(10,3), 2)
            ELSE a.mnozstvi
        END AS mnozstvi,
    a.cen_hlad,
    a.k_odpoctu,
    a.poradi,
    a.dotacia,
    a.dil_porce,
    a.id_fmenu,
    a.c_data,
    a.kompez,
    a.stor_mno,
    a.id_karty,
        CASE
            WHEN b.prachy_sum <> 0::numeric THEN round((a.sleva_pol * b.cena_pl / b.prachy_sum)::numeric(12,2), 2)
            ELSE a.sleva_pol
        END AS sleva_pol,
    a.sleva_txt,
    a.jc,
    a.id_rov,
    a.idriadok,
    a.c_druh,
    a.nazev,
    b.druh_pl
   FROM food600.ssk__pol a
     JOIN food600.ssk__pltsumy b ON a.c_uzaverka = b.c_uzaverka AND a.ucet = b.ucet
  ORDER BY a.c_uzaverka, a.ucet, a.idriadok;

