INSERT INTO us3
(nr01, p01, p02, p03, p04, 
remdate, remtime, remuser, remfdate, remftime, remfuser)

select  peg_new_nr01('us3', 1) as nr01,
        us0.p01 as p01,
        us4.p02 as p02,
        us4.p01 as p03,
        us0.p410 as p04, 
        curdate() as remdate, 
        curtime() as remtime, 
        '{user_pswd}' as remuser, 
        curdate() as remfdate, 
        curtime() as remftime, 
        '{user_pswd}' as remfuser 
FROM US4
LEFT JOIN d24 ON d24.p02=us4.p01
LEFT JOIN us0 ON us0.p410=d24.p01
LEFT JOIN us3 ON us3.p01=us0.p01 && us3.p02=us4.p02
WHERE d24.p02='{np01}'
   && us3.nr01 is null
GROUP BY us0.p01, us4.p02;
          
