CREATE OR REPLACE VIEW food600.szadanka2nakoupeno
 AS
 SELECT aaa.c_data,
    aaa.c_stredisk,
    aaa.c_karty,
    doklady.c_partner,
    obraty.cena,
    aaa.c_dokl,
    doklady.datum_dokl
   FROM ( SELECT obraty_1.c_data,
            obraty_1.c_stredisk,
            obraty_1.c_karty,
            doklady_1.c_partner,
            max(obraty_1.c_dokl) AS c_dokl
           FROM food600.obraty obraty_1
             JOIN food600.doklady doklady_1 ON obraty_1.c_dokl = doklady_1.c_dokl AND obraty_1.c_data = doklady_1.c_data
          WHERE obraty_1.operace = 'PE'::bpchar
          GROUP BY obraty_1.c_data, obraty_1.c_stredisk, obraty_1.c_karty, doklady_1.c_partner) aaa
     JOIN food600.obraty ON aaa.c_dokl = obraty.c_dokl AND aaa.c_karty = obraty.c_karty AND aaa.c_data = obraty.c_data
     JOIN food600.doklady ON aaa.c_dokl = doklady.c_dokl AND aaa.c_data = doklady.c_data;
