select
    ifnull(db5.nr01, 0) as nr01,
    db2.nr01 as p02,
    db5.p03 as p03,
    db5.p04 as p04,
    db5.p05 as p05,
    db2.p01  as db2p01,
    db2.p02  as db2p02,
    db2.p03  as db2p03,
    db2.p04  as db2p04,
    db2.p05  as db2p05,
    db2.p06  as db2p06,
    db2.p07  as db2p07,
    db2.p010 as db2p010,
    db2.p011 as db2p011,
    db2.p30  as db2p30,
    db5.p03  as vp03,
    db3.p02  as db3p02,
    db4.p03  as db4p03
FROM db2
LEFT JOIN db5 ON db2.nr01=db5.p02
              && db5.p01='{db6trn->nr01}'
LEFT JOIN db4 ON db4.nr01=db5.p04
LEFT JOIN db3 ON db3.nr01=db2.p07

WHERE db2.p01='{ndb6p01}'
   && (    db2.p09=1
        || (    db2.p08>={peg_ketefrom}
             && db2.p08<={peg_keteto}
           )
      );
