DO
$$
BEGIN

IF NOT EXISTS (SELECT column_name FROM information_schema.columns WHERE table_schema='food600' and table_name='fras_set' and column_name='iddruh') THEN
CREATE OR REPLACE VIEW food600.skasfras_set
 AS
 SELECT fras_set.rastr,
    fras_set.rastr1,
    fras_set.rastr2,
    fras_set.rastr3,
    fras_set.rastr4,
    fras_set.rastr5,
    fras_set.rastr6,
    fras_set.rastr7,
    fras_set.rastr8,
    fras_set.rastr9,
    fras_set.kdy,
    fras_set.kdo,
    fras_set.c_druh,
    fras_set.id,
    skupiny.druh::character(30)
   FROM food600.fras_set
     LEFT JOIN food600.skupiny ON fras_set.c_druh = skupiny.c_druh AND fras_set.c_data = skupiny.c_data
  WHERE fras_set.c_druh <> (-1) AND fras_set.c_druh <> 0
UNION
 SELECT fras_set.rastr,
    fras_set.rastr1,
    fras_set.rastr2,
    fras_set.rastr3,
    fras_set.rastr4,
    fras_set.rastr5,
    fras_set.rastr6,
    fras_set.rastr7,
    fras_set.rastr8,
    fras_set.rastr9,
    fras_set.kdy,
    fras_set.kdo,
    fras_set.c_druh,
    fras_set.id,
    'TIP'::character(30) AS druh
   FROM food600.fras_set
  WHERE fras_set.c_druh = (-1);

ELSE
CREATE OR REPLACE VIEW food600.skasfras_set
 AS
 SELECT fras_set.rastr,
    fras_set.rastr1,
    fras_set.rastr2,
    fras_set.rastr3,
    fras_set.rastr4,
    fras_set.rastr5,
    fras_set.rastr6,
    fras_set.rastr7,
    fras_set.rastr8,
    fras_set.rastr9,
    fras_set.kdy,
    fras_set.kdo,
    fras_set.c_druh,
    fras_set.id,
    skupiny.druh::character(30),
    fras_set.iddruh
   FROM food600.fras_set
     LEFT JOIN food600.skupiny ON fras_set.c_druh = skupiny.c_druh AND fras_set.c_data = skupiny.c_data
  WHERE fras_set.c_druh <> (-1) AND fras_set.c_druh <> 0
UNION
 SELECT fras_set.rastr,
    fras_set.rastr1,
    fras_set.rastr2,
    fras_set.rastr3,
    fras_set.rastr4,
    fras_set.rastr5,
    fras_set.rastr6,
    fras_set.rastr7,
    fras_set.rastr8,
    fras_set.rastr9,
    fras_set.kdy,
    fras_set.kdo,
    fras_set.c_druh,
    fras_set.id,
    'TIP'::character(30) AS druh,
    fras_set.iddruh
   FROM food600.fras_set
  WHERE fras_set.c_druh = (-1);
END IF;
END
$$
