经费统计sql

select '合计' as orgname,
       '' as jgxz,
       count(o.orgid) as jgs,
       nvl(sum((select nsjgs
                 from (select r.orgid, r.nsjgs
                         from Taizhang r
                        order by r.pzsj desc, r.tjsj desc)
                where orgid = o.orgid
                  and rownum = 1)),
           0) as nsjgs,
       nvl(sum((select qebkrys
                  from (select r.orgid, r.qebkrys
                          from Taizhang r
                         order by r.pzsj desc, r.tjsj desc)
                 where orgid = o.orgid
                   and rownum = 1) +
               (select cebkrys
                  from (select r.orgid, r.cebkrys
                          from Taizhang r
                         order by r.pzsj desc, r.tjsj desc)
                 where orgid = o.orgid
                   and rownum = 1) +
               (select jfzlrys
                  from (select r.orgid, r.jfzlrys
                          from Taizhang r
                         order by r.pzsj desc, r.tjsj desc)
                 where orgid = o.orgid
                   and rownum = 1) +
               (select qyhglrys
                  from (select r.orgid, r.qyhglrys
                          from Taizhang r
                         order by r.pzsj desc, r.tjsj desc)
                 where orgid = o.orgid
                   and rownum = 1)),
           0) as hj,
       nvl(sum((select qebkrys
                 from (select r.orgid, r.qebkrys
                         from Taizhang r
                        order by r.pzsj desc, r.tjsj desc)
                where orgid = o.orgid
                  and rownum = 1)),
           0) as qebkbzs,
       nvl(sum((select cebkrys
                 from (select r.orgid, r.cebkrys
                         from Taizhang r
                        order by r.pzsj desc, r.tjsj desc)
                where orgid = o.orgid
                  and rownum = 1)),
           0) as cebkbzs,
       nvl(sum((select jfzlrys
                 from (select r.orgid, r.jfzlrys
                         from Taizhang r
                        order by r.pzsj desc, r.tjsj desc)
                where orgid = o.orgid
                  and rownum = 1)),
           0) as jfzlbzs,
       nvl(sum((select qyhglrys
                 from (select r.orgid, r.qyhglrys
                         from Taizhang r
                        order by r.pzsj desc, r.tjsj desc)
                where orgid = o.orgid
                  and rownum = 1)),
           0) as qyhglbzs,
       nvl(sum((select count(p.id)
                  from Person p
                 where p.szjg = o.orgid
                   and kzqd = '11'
                   and rylb = '10') +
               (select count(p.id)
                  from Person p
                 where p.szjg = o.orgid
                   and kzqd = '12'
                   and rylb = '10') +
               (select count(p.id)
                  from Person p
                 where p.szjg = o.orgid
                   and kzqd = '2'
                   and rylb = '10') +
               (select count(p.id)
                  from Person p
                 where p.szjg = o.orgid
                   and kzqd = '3'
                   and rylb = '10')),
           0) as hj2,
       nvl(sum((select count(p.id)
                 from Person p
                where p.szjg = o.orgid
                  and kzqd = '11'
                  and rylb = '10')),
           0) as qebksys,
       nvl(sum((select count(p.id)
                 from Person p
                where p.szjg = o.orgid
                  and kzqd = '12'
                  and rylb = '10')),
           0) as cebksys,
       nvl(sum((select count(p.id)
                 from Person p
                where p.szjg = o.orgid
                  and kzqd = '2'
                  and rylb = '10')),
           0) as jfzlsys,
       nvl(sum((select count(p.id)
                 from Person p
                where p.szjg = o.orgid
                  and kzqd = '3'
                  and rylb = '10')),
           0) as qyhglsys,
       nvl(sum((select count(p.id)
                 from Person p
                where p.szjg = o.orgid
                  and ryjg = '11'
                  and p.rylb = '10')),
           0) as xzglrys,
       nvl(sum((select count(p.id)
                 from Person p
                where p.szjg = o.orgid
                  and ryjg = '12'
                  and p.rylb = '10')),
           0) as zyjsrys,
       nvl(sum((select count(p.id)
                 from Person p
                where p.szjg = o.orgid
                  and ryjg = '13'
                  and p.rylb = '10')),
           0) as scgrrys,
       nvl(sum((select count(p.id)
                 from Person p
                where p.szjg = o.orgid
                  and ryjg = '14'
                  and p.rylb = '10')),
           0) as hqrys,
       nvl(sum((select count(p.id)
                 from Person p
                where p.szjg = o.orgid
                  and ryjg = '15'
                  and p.rylb = '10')),
           0) as qtrys
  from organization o
 where o.orgid in (select orgid
                     from organization
                    where qhdm = '220221'
                      and qfbs = '1')

 

你可能感兴趣的:(sql)