All pastes #161248 Raw Edit

Gopneg

public text v1 · immutable
·published 2006-09-05 05:32 UTC
rendered paste body
create or replace view AccRegistrOtklAmortCore as
select
  amrt.nminref amort,                               -- аморт группа
  amrtcd.codinref amortcode,                        -- код аморт                                                                                              
  dr.nminref dr,                                    -- дох/расх
  drcd.codinref drcd,                               -- код дох/расх    
  st.nminref stat,                                  -- статья дох/расх
  t.codinref2201 oscode,                            -- код статьи дох/расх  
  decode(t.acc1, 2, 'Спис. аморт. при выб. (Д02-К01)',
                 1, 'Нач. аморт. (Д..- К02)', '!Error!') acc, -- начисление/амортизация 
  t.acc1 acccode,                                   -- код счета
  sum(                                              -- до периода - остатки    
    /**decode(t.acc1, 1, /**/
      - decode(
        ith_misc_func.dateinrange(
          to_date(p.DateFr, 'dd.mm.yyyy'), 
          to_date(p.DateTo, 'dd.mm.yyyy'),
          t.datetr), 
        0, 
        decode( t.flagcredit, 0, t.summaincur, 0 ) -
        decode( t.flagcredit, 1, t.summaincur, 0 ),
        0    
      )/**,
      - decode(
        ith_misc_func.dateinrange(
          to_date(p.DateFr, 'dd.mm.yyyy'), 
          to_date(p.DateTo, 'dd.mm.yyyy'),
          t.datetr), 
        0, 
        decode( t.flagcredit, 0, t.summaincur, 0 ) -
        decode( t.flagcredit, 1, t.summaincur, 0 ),
       0    
      )
    ) /**/
  ) sumost,    
  sum(                                              -- в периоде - обороты
    decode(t.acc1, 1, 
      decode(
        ith_misc_func.dateinrange(
          to_date(p.DateFr, 'dd.mm.yyyy'), 
          to_date(p.DateTo, 'dd.mm.yyyy'),
          t.datetr), 
        1, 
        decode( t.flagcredit, 0, t.summaincur, 0 ) -
        decode( t.flagcredit, 1, t.summaincur, 0 ),
        0    
      ),
      - decode(
        ith_misc_func.dateinrange(
          to_date(p.DateFr, 'dd.mm.yyyy'), 
          to_date(p.DateTo, 'dd.mm.yyyy'),
          t.datetr), 
        1, 
        decode( t.flagcredit, 0, t.summaincur, 0 ) -
        decode( t.flagcredit, 1, t.summaincur, 0 ),
        0    
      ) 
    )
  ) summain,
  t.codplant codplant,
  t.acc1 acc1,
  i.perc / 100 perc
  
from acctransreportview t, 
     refgroupextent amrt, 
     refitemref amrtcd, 
     refgroupextent dr,
     refitemref drcd, 
     refuser st, 
     ithcde_oso_sngprm p,     
     acc acc,
     (
     select i1.codinref, i1.value datebeg, i2.value dateend, i3.value perc 
     from refitemattr i1, refitemattr i2, refitemattr i3
     where i1.codref = 32201 
       and i1.codattr = 64401
       and i2.codref = i1.codref
       and i2.codattr = 64501      
       and i3.codref = i1.codref
       and i3.codattr = 64601              
       and i2.codinref = i1.codinref
       and i3.codinref = i1.codinref
     ) i

where t.datetr <= to_date(p.DateTo, 'dd.mm.yyyy')                                 
  and ith_misc_func.dateinrange(
        to_date(i.datebeg, 'dd/mm/yyyy'),
        to_date(i.dateend, 'dd/mm/yyyy'),
        t.datetr) = 1     
  and t.codbr = p.CodBr--201  
  and
  (  
    -- &#1041;&#1091;&#1093;&#1075;&#1072;&#1083;&#1090;&#1077;&#1088;&#1089;&#1082;&#1080;&#1081; &#1059;&#1095;&#1077;&#1090; 
    (     
          t.codplan  = 501
	    and t.codplant = 201
      and
      (
        (t.acc1 = 02 and t.acc2 <> 02) 
        or
        (t.acc1 = 01)
      )
    ) 
    or  
    -- &#1053;&#1072;&#1083;&#1086;&#1075;&#1086;&#1074;&#1099;&#1081; &#1059;&#1095;&#1077;&#1090; 
    (
          t.codplan  = 2201 
      and t.codplant = 2301 
      and
      (
        (t.acc1 = 02 and t.acc2 <> 02) 
        or
        (t.acc1 = 01)
      )
    )    
  )

  and t.flagofficial = 1      
  and t.summaincur <> 0  
  
  and st.codinref = t.codinref2201           
  and st.codref = 2201  

  and amrtcd.codinrk (+) = t.codinref2201   
  and amrtcd.codrk (+) = 2201   
  and amrtcd.codref (+) = 18301 
  
  and amrt.codinref (+) = amrtcd.codinref  
	and amrt.codref (+) = 18301                            
  
  and drcd.codinrk (+) = t.codinref2201                        
  and drcd.codrk (+) = 2201   
  and drcd.codref (+) = 18401 
  
  and dr.codinref (+) = drcd.codinref 
  and dr.codref (+) = 18401 
    
  and acc.acc (+) = t.acc1  
  and acc.codplant (+) = 201   
  and acc.subacc (+) = 0 
  
group by amrt.nminref, amrtcd.codinref, dr.nminref, drcd.codinref, t.codinref2201, 
         decode(t.acc1, 2, '&#1057;&#1087;&#1080;&#1089;. &#1072;&#1084;&#1086;&#1088;&#1090;. &#1087;&#1088;&#1080; &#1074;&#1099;&#1073;. (&#1044;02-&#1050;01)',
                        1, '&#1053;&#1072;&#1095;.  &#1072;&#1084;&#1086;&#1088;&#1090;. (&#1044;..- &#1050;02)', '!Error!'),
         t.acc1, st.nminref, t.codplant, t.acc1, i.perc / 100
order by amort, dr, stat, acc, sumost, summain
         
         
---------------------

create or replace view AccRegistrOtklAmort as
select
  s.amort amort,
  s.amortcode amortcode, 
  s.dr accgr, 
  s.drcd drcd, 
  s.stat stat,
  s.oscode oscode,   
  s.acc acc,        
  s.acccode acccode,             
  -- &#1086;&#1089;&#1090;&#1072;&#1090;&#1082;&#1080; 
  sum(s.sumost_bu) sumost_bu,
  sum(s.sumost_nu) sumost_nu,
  sum(s.sumost_otkl) sumost_otkl,
  sum(s.sumost_ona) sumost_ona,  
  sum(s.sumost_ono) sumost_ono,
  
  -- &#1086;&#1073;&#1086;&#1088;&#1086;&#1090;&#1099;
  sum(s.summain_bu) summain_bu,
  sum(s.summain_nu) summain_nu,
  sum(s.summain_otkl) summain_otkl,  
  sum(s.summain_ona) summain_ona,
  sum(s.summain_ono) summain_ono,
  
  -- &#1089;&#1072;&#1083;&#1100;&#1076;&#1086;
  sum(s.sumsald_bu) sumsald_bu,
  sum(s.sumsald_nu) sumsald_nu,
  sum(s.sumsald_otkl) sumsald_otkl,
  sum(s.sumsald_ona) sumsald_ona,
  sum(s.sumsald_ono) sumsald_ono
  
  /**--------------- &#1043;&#1088;&#1091;&#1087;&#1087;&#1086;&#1074;&#1099;&#1077; &#1089;&#1091;&#1084;&#1084;&#1099; &#1087;&#1086; &#1089;&#1087;&#1080;&#1089;&#1072;&#1085;&#1080;&#1102;
  -- &#1086;&#1089;&#1090;&#1072;&#1090;&#1082;&#1080; 
  sum(decode(s.acc1, 1, s.sumost_bu, 0)) sumost_bu_s,
  sum(decode(s.acc1, 1, s.sumost_nu, 0)) sumost_nu_s,
  sum(decode(s.acc1, 1, s.sumost_otkl, 0)) sumost_otkl_s,
  sum(decode(s.acc1, 1, s.sumost_ona, 0)) sumost_ona_s,  
  sum(decode(s.acc1, 1, s.sumost_ono, 0)) sumost_ono_s,
  
  -- &#1086;&#1073;&#1086;&#1088;&#1086;&#1090;&#1099;
  sum(decode(s.acc1, 1, s.summain_bu, 0)) summain_bu_s,
  sum(decode(s.acc1, 1, s.summain_nu, 0)) summain_nu_s,
  sum(decode(s.acc1, 1, s.summain_otkl, 0)) summain_otkl_s,  
  sum(decode(s.acc1, 1, s.summain_ona, 0)) summain_ona_s,
  sum(decode(s.acc1, 1, s.summain_ono, 0)) summain_ono_s,
  
  -- &#1089;&#1072;&#1083;&#1100;&#1076;&#1086;
  sum(decode(s.acc1, 1, s.sumsald_bu, 0)) sumsald_bu_s,
  sum(decode(s.acc1, 1, s.sumsald_nu, 0)) sumsald_nu_s,
  sum(decode(s.acc1, 1, s.sumsald_otkl, 0)) sumsald_otkl_s,
  sum(decode(s.acc1, 1, s.sumsald_ona, 0)) sumsald_ona_s,
  sum(decode(s.acc1, 1, s.sumsald_ono, 0)) sumsald_ono_s /**/
    
from (
  select 
    v.amort amort,
    v.amortcode amortcode, 
    v.dr dr, 
    v.drcd drcd, 
    v.stat stat,
    v.oscode oscode ,   
    v.acc acc,        
    v.acccode acccode,             
    -- &#1086;&#1089;&#1090;&#1072;&#1090;&#1082;&#1080; 
    sum(v.sumost_bu) sumost_bu,
    sum(v.sumost_nu) sumost_nu,
    sum(v.sumost_otkl) sumost_otkl,
    sum(
      decode( 
        decode(                    
          decode(sumost_bu * sumost_nu, 0, 0, 1),       -- &#1077;&#1089;&#1083;&#1080; &#1041;&#1059; &#1080; &#1053;&#1059; &#1085;&#1077; &#1088;&#1072;&#1074;&#1085;&#1099; &#1085;&#1091;&#1083;&#1102; - 1, &#1080;&#1085;&#1072;&#1095;&#1077; - 0
          1,
          ith_misc_func.numbinrange(0, 0, sumost_otkl), -- &#1077;&#1089;&#1083;&#1080; &#1086;&#1090;&#1082;&#1083;&#1086;&#1085;&#1077;&#1085;&#1080;&#1077; &#1073;&#1086;&#1083;&#1100;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103; &#1090;&#1086; 2, &#1080;&#1085;&#1072;&#1095;&#1077; 0
          1),                                           -- &#1077;&#1089;&#1083;&#1080; 2 - &#1054;&#1053;&#1040;, 0 - &#1054;&#1053;&#1054;, 1 - &#1088;&#1077;&#1096;&#1072;&#1077;&#1084; &#1076;&#1072;&#1083;&#1100;&#1096;&#1077;
        2,
        v.sumost_otkl * v.perc,                         -- &#1089;&#1091;&#1084;&#1084;&#1072;  
        0,
        0,                                              -- &#1089;&#1091;&#1084;&#1084;&#1072;  
        decode(    
          ith_misc_func.numbinrange(
            0, 0, sumost_bu * sumost_bu),               -- &#1077;&#1089;&#1083;&#1080; &#1074;&#1099;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103; - 2, &#1088;&#1072;&#1074;&#1085;&#1086; - 1, &#1085;&#1080;&#1078;&#1077; &#1085;&#1091;&#1083;&#1103; - 0
          1,                                            -- &#1077;&#1089;&#1083;&#1080; &#1041;&#1059; = 0 &#1090;&#1086; &#1054;&#1053;&#1040;
          v.sumost_otkl * v.perc,                       -- &#1089;&#1091;&#1084;&#1084;&#1072;  
          0)
    )) sumost_ona,
    
    sum(
      decode( 
        decode(                    
          decode(sumost_bu * sumost_nu, 0, 0, 1),       -- &#1077;&#1089;&#1083;&#1080; &#1041;&#1059; &#1080; &#1053;&#1059; &#1085;&#1077; &#1088;&#1072;&#1074;&#1085;&#1099; &#1085;&#1091;&#1083;&#1102; - 1, &#1080;&#1085;&#1072;&#1095;&#1077; - 0
          1,
          ith_misc_func.numbinrange(0, 0, sumost_otkl), -- &#1077;&#1089;&#1083;&#1080; &#1086;&#1090;&#1082;&#1083;&#1086;&#1085;&#1077;&#1085;&#1080;&#1077; &#1073;&#1086;&#1083;&#1100;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103; &#1090;&#1086; 2, &#1080;&#1085;&#1072;&#1095;&#1077; 0
          1),                                           -- &#1077;&#1089;&#1083;&#1080; 2 - &#1054;&#1053;&#1040;, 0 - &#1054;&#1053;&#1054;, 1 - &#1088;&#1077;&#1096;&#1072;&#1077;&#1084; &#1076;&#1072;&#1083;&#1100;&#1096;&#1077;
        0,
        v.sumost_otkl * v.perc,                         -- &#1089;&#1091;&#1084;&#1084;&#1072;  
        2,
        0,                                              -- &#1089;&#1091;&#1084;&#1084;&#1072;  
        decode(    
          ith_misc_func.numbinrange(
            0, 0, sumost_nu * sumost_nu),               -- &#1077;&#1089;&#1083;&#1080; &#1074;&#1099;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103; - 2, &#1088;&#1072;&#1074;&#1085;&#1086; - 1, &#1085;&#1080;&#1078;&#1077; &#1085;&#1091;&#1083;&#1103; - 0
          1,                                            -- &#1077;&#1089;&#1083;&#1080; &#1053;&#1059; = 0 &#1090;&#1086; &#1054;&#1053;&#1040;
          v.sumost_otkl * v.perc,                       -- &#1089;&#1091;&#1084;&#1084;&#1072;  
          0)
    )) sumost_ono,
    
    -- &#1086;&#1073;&#1086;&#1088;&#1086;&#1090;&#1099;
    sum(v.summain_bu) summain_bu,
    sum(v.summain_nu) summain_nu,
    sum(v.summain_otkl) summain_otkl,  
    sum(
      decode(
        ith_misc_func.numbinrange(0, 0, abs(v.summain_bu) - abs(v.summain_nu)),
        decode(v.acccode, 1, 0, 2),  -- 0 - &#1084;&#1077;&#1085;&#1100;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103;, 2 - &#1073;&#1086;&#1083;&#1100;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103;, 1 = &#1085;&#1091;&#1083;&#1102; 
        -- &#1085;&#1072; &#1086;&#1089; &#1085;&#1072;&#1076;&#1086; &#1085;&#1072; &#1086;&#1085;&#1086; &#1077;&#1089;&#1083;&#1080; &#1073;&#1091; > &#1085;&#1091; &#1085;&#1072; &#1072;&#1084;&#1086;&#1088;&#1090; &#1085;&#1072;&#1076;&#1086; &#1085;&#1072; &#1086;&#1085;&#1072;
        decode(v.summain_bu * v.summain_nu, 0, -1, 1) -- &#1079;&#1085;&#1072;&#1082; &#1086;&#1085;&#1072;/&#1086;&#1085;&#1086;
        * v.summain_otkl * v.perc,
        0)
    ) summain_ona,
    sum(
      decode(
        ith_misc_func.numbinrange(0, 0, abs(v.summain_bu) - abs(v.summain_nu)),
        decode(v.acccode, 1, 2, 0),  -- 0 - &#1084;&#1077;&#1085;&#1100;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103;, 2 - &#1073;&#1086;&#1083;&#1100;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103;, 1 = &#1085;&#1091;&#1083;&#1102; 
        -- &#1085;&#1072; &#1086;&#1089; &#1085;&#1072;&#1076;&#1086; &#1085;&#1072; &#1086;&#1085;&#1086; &#1077;&#1089;&#1083;&#1080; &#1073;&#1091; > &#1085;&#1091; &#1085;&#1072; &#1072;&#1084;&#1086;&#1088;&#1090; &#1085;&#1072;&#1076;&#1086; &#1085;&#1072; &#1086;&#1085;&#1072;
        decode(v.summain_bu * v.summain_nu, 0, -1, 1) -- &#1079;&#1085;&#1072;&#1082; &#1086;&#1085;&#1072;/&#1086;&#1085;&#1086;
        * v.summain_otkl * v.perc,
        0)
    ) summain_ono,
    
    -- &#1089;&#1072;&#1083;&#1100;&#1076;&#1086;
    sum(v.sumsald_bu) sumsald_bu,
    sum(v.sumsald_nu) sumsald_nu,
    sum(v.sumsald_otkl) sumsald_otkl,
    sum(
      decode(
        ith_misc_func.numbinrange(0, 0, abs(v.sumsald_bu) - abs(v.sumsald_nu)),
        decode(v.acccode, 1, 0, 2),  -- 0 - &#1084;&#1077;&#1085;&#1100;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103;, 2 - &#1073;&#1086;&#1083;&#1100;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103;, 1 = &#1085;&#1091;&#1083;&#1102; 
        -- &#1085;&#1072; &#1086;&#1089; &#1085;&#1072;&#1076;&#1086; &#1085;&#1072; &#1086;&#1085;&#1086; &#1077;&#1089;&#1083;&#1080; &#1073;&#1091; > &#1085;&#1091; &#1085;&#1072; &#1072;&#1084;&#1086;&#1088;&#1090; &#1085;&#1072;&#1076;&#1086; &#1085;&#1072; &#1086;&#1085;&#1072;
        decode(v.sumsald_bu * v.sumsald_nu, 0, -1, 1) -- &#1079;&#1085;&#1072;&#1082; &#1086;&#1085;&#1072;/&#1086;&#1085;&#1086;
        * v.sumsald_otkl * v.perc,
        0)
    ) sumsald_ona,
    sum(
      decode(
        ith_misc_func.numbinrange(0, 0, abs(v.sumsald_bu) - abs(v.sumsald_nu)),
        decode(v.acccode, 1, 2, 0),  -- 0 - &#1084;&#1077;&#1085;&#1100;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103;, 2 - &#1073;&#1086;&#1083;&#1100;&#1096;&#1077; &#1085;&#1091;&#1083;&#1103;, 1 = &#1085;&#1091;&#1083;&#1102; 
        -- &#1085;&#1072; &#1086;&#1089; &#1085;&#1072;&#1076;&#1086; &#1085;&#1072; &#1086;&#1085;&#1086; &#1077;&#1089;&#1083;&#1080; &#1073;&#1091; > &#1085;&#1091; &#1085;&#1072; &#1072;&#1084;&#1086;&#1088;&#1090; &#1085;&#1072;&#1076;&#1086; &#1085;&#1072; &#1086;&#1085;&#1072;
        decode(v.sumsald_bu * v.sumsald_nu, 0, -1, 1) -- &#1079;&#1085;&#1072;&#1082; &#1086;&#1085;&#1072;/&#1086;&#1085;&#1086;
        * v.sumsald_otkl * v.perc,
        0)
    ) sumsald_ono,
    v.codplant codplant,
    v.acc1 acc1
    
  from (
    select 
      a.amort amort,
      a.amortcode amortcode, 
      a.dr dr, 
      a.drcd drcd, 
      a.stat stat,
      a.oscode oscode ,   
      a.acc acc,        
      a.acccode acccode,
      sum(a.sumost_bu) sumost_bu,
      sum(a.sumost_nu) sumost_nu,
      sum(abs(a.sumost_bu) - abs(a.sumost_nu)) sumost_otkl,
      
      sum(a.summain_bu) summain_bu,
      sum(a.summain_nu) summain_nu,
      sum(abs(a.summain_bu) - abs(a.summain_nu)) summain_otkl,
      
      sum(a.sumost_bu + a.summain_bu) sumsald_bu,
      sum(a.sumost_nu + a.summain_nu) sumsald_nu,
      sum(abs(a.sumost_bu) + abs(a.summain_bu) - (abs(a.sumost_nu) + abs(a.summain_nu))) sumsald_otkl,
      a.codplant codplant,
      a.acc1 acc1,
      a.perc perc
      
    from (
      select 
        t.amort amort,
        t.amortcode amortcode, 
        t.dr dr, 
        t.drcd drcd, 
        t.stat stat,
        t.oscode oscode ,   
        t.acc acc,        
        t.acccode acccode,
        decode(t.codplant, 201,  t.sumost, 0) sumost_bu,
        decode(t.codplant, 2301, t.sumost, 0) sumost_nu,
      
        decode(t.codplant, 201,  t.summain, 0) summain_bu,
        decode(t.codplant, 2301, t.summain, 0) summain_nu,
      
        t.codplant codplant,
        t.acc1 acc1,
        t.perc perc
    
      from accregistrotklamortcore t) a
      
    group by a.amort, a.amortcode, a.dr, a.drcd, a.stat, a.oscode, a.acc, a.acccode, a.codplant, a.acc1, a.perc) v

  group by v.amort, v.amortcode, v.dr, v.drcd, v.stat, v.oscode, v.acc, v.acccode, v.codplant, v.acc1) s 

group by s.amort, s.amortcode, s.dr, s.drcd, s.stat, s.oscode, s.acc, s.acccode
order by amort, accgr, stat, acc