SELECT *
FROM 
(

    select p07.p01 as p01,
           p07.p02 as p02,
           p07.p03 as p03,
           p07.p05 as p05,
           p07.p06 as p06,
           0 as p07, 
           ifnull(w03.p07, '') as p08, 
           p07.p30 as p09, 
           ifnull(w03.nr01, 0) as p010, 
           p07.remdate as remdate, 
           p07.remtime as remtime, 
           p07.remuser as remuser, 
           p07.remfdate as remfdate, 
           p07.remftime as remftime, 
           p07.remfuser as remfuser, 
           p07.remguc as remguc 
           
    FROM {peg_str1} as p07
    LEFT JOIN w03 ON replace(w03.p02, '\\', '')=replace(p07.p06, '\\', '')
    WHERE p07.p102=1
    
    /****************************************
           l00 tables (from p07_1)
    ****************************************/
{if( odbcistable('l00'), '', '/* logi')}
    UNION ALL 
    
    select concat(p07.p01, '_', cast(l00.nr01 as char)) as p01,
           p07.p02 as p02,
           concat(p07.p03, ' (', l00.p02, ') ') as p03,
           p07.p05 as p05,
           p07.p06 as p06,
           0 as p07, 
           ifnull(w03.p07, '') as p08, 
           p07.p30 as p09, 
           ifnull(w03.nr01, 0) as p010,
           l00.remdate as remdate, 
           l00.remtime as remtime, 
           l00.remuser as remuser, 
           l00.remfdate as remfdate, 
           l00.remftime as remftime, 
           l00.remfuser as remfuser, 
           l00.remguc as remguc 
           
    FROM {peg_str1} as p07
    LEFT JOIN l00 ON l00.nr01>1
    LEFT JOIN w03 ON replace(w03.p02, '\\', '')=replace(p07.p06, '\\', '')
    WHERE p07.p102=1
       && p07.p01 in ('l92', 'l93', 'l80')
       && l00.nr01 is not null
{if( odbcistable('l00'), '', 'logi*/')}
    
) as tbl01

WHERE {peg_str};