/*   beg_count  -  end_count are for count query
     beg_rec    -  end_rec   are for records  
     beg_katast -  end_katast   are for katastaseis  */

select
[beg_count]    count(*) as plit,  [end_count]
    d71.nr01 as nr01,
    d71.p02  as p02,
    d71.p03  as p03,
    d71.p04  as p04,
    d71.p06  as p06,
    d71.p07  as p07,
    d71.p08  as p08,
    d71.p011 as p011,
    d71.p016 as p016,
    ifnull(d59.p00, 0) as vp016, 
    us0.p100 as vp011,
    d71.p014 as p014,
    d71.p015 as p015,
    d48.p02  as vp014,
    d49.p02  as vp015, 
    d83.p01  as vp_kat, 
    d51.p02  as vp_vkat


from d71
LEFT JOIN d48 ON d48.p01=d71.p014
LEFT JOIN d49 ON d49.p01=d71.p015
LEFT JOIN us0 ON us0.p01=d71.p011
LEFT JOIN d83 ON d83.p01=d71.nr01
LEFT JOIN d51 ON d51.p01=d83.p02
LEFT JOIN d59 ON d59.nr01=d71.p016
              
WHERE {p_d71cond1}
   && {p_d71cond2}
   && {p_d71cond3}
   && {kat}
   && {ypokat}
   && {skopos}
   && {syg}
   && d71.p07>='{odbcdtos(nv_ch1)}'
   && d71.p07<='{odbcdtos(nv_ch2)}'   
   && d71.p018>='{odbcdtos(nv_da1)}'
   && d71.p018<='{odbcdtos(nv_da2)}'   
   && {katast}
   {lhmma}

[beg_count] GROUP BY NULL [end_count]

[beg_rec]   ORDER BY d71.p07 desc
            LIMIT {(np_aspage-1) * np_aslim}, {np_aslim} [end_rec] 
            
            ;
