select
    nr01 as nr01,
    p20  as p20,
    p19  as p19,
    1    as pknd
FROM

(

select
    t1.nr01 as nr01,
    t1.p20 as p20,
    t1.p19 as p19,
    t1.sum1 as sum1,
    t1.sum1/11 as sum21,
    t1.sum1 div 11 as sum22,
    (t1.sum1 div 11)*11 as sum23,
    t1.sum1-(t1.sum1 div 11)*11 as sum24,
    (t1.sum1-(t1.sum1 div 11)*11) mod 10 as sum25,
    t1.v9 as v9
    
FROM

(

 
select
   a01.nr01 as nr01,
   a01.p20 as p20,
   a01.p19 as p19,
   cast( substr(a01.p20, 1, 1) as unsigned) * 256
 + cast( substr(a01.p20, 2, 1) as unsigned) * 128
 + cast( substr(a01.p20, 3, 1) as unsigned) * 64
 + cast( substr(a01.p20, 4, 1) as unsigned) * 32
 + cast( substr(a01.p20, 5, 1) as unsigned) * 16
 + cast( substr(a01.p20, 6, 1) as unsigned) * 8
 + cast( substr(a01.p20, 7, 1) as unsigned) * 4
 + cast( substr(a01.p20, 8, 1) as unsigned) * 2   as sum1,

   cast( substr(a01.p20, 9, 1) as unsigned) as v9

from a01
where p05=1


) as t1

) as t2
WHERE v9<>sum25
   || p20=' '
   || length(p20)<>9 ;
