Селект

Печать: Шрифт: Абв Абв Абв
admin 08 Апреля 2008 в 17:23:51
$sql="
select r.stat,
r.fider,
r.name tp_ktp,
count(c.id) kol,
sum(o.osn_sum) osn_opl,
sum(trunc((o.osn_sum) / v.gt, 0)) osn_oplkwt,
sum(o.peni_sum) peni,
sum(o.dop_sum) dop_opl,
sum(trunc((o.dop_sum) / v.gt, 0)) dop_oplkwt,
sum(o.ndy) ndy,
sum(trunc((o.ndy) / v.gt, 0)) ndykwt ,
sum(o.podkl) podkl,
sum(o.akt) akt,
sum(trunc((o.akt) / v.gt, 0)) aktkwt,
sum(o.svarka) svarka,
sum(trunc((o.svarka) / v.gt, 0)) svarkakwt,
sum(o.proch) proch,
sum(trunc((o.proch) / v.gt, 0)) prochkwt,

sum(o.all_sum) all_opl,
sum(nvl(sch.kol, 0)) kol_obh,
sum(nvl(sch.kvt, 0)) sch_kwt

from client c,

(select t.summa gt
from tariff t
where t.del > 0 and (id_tariff_code = 1) and (id_client_type = 0) and
(id_client_subtype = 0) and (id_istoch = 0) and
(id_ocrug = 0) and
nvl(lastday('".$arx."'), sysdate) between dbeg and
decode(dend, null, nvl(lastday('".$arx."'), sysdate) + 1, dend)) v,

(select ls,
1 kol,
sum(decode(sign(out_sch - in_sch),
-1,
(sch_raz(sch.id)),
out_sch - in_sch)) kvt

--,in_sch,out_sch, out_date
from sch
where del > 0 and in_sch <> out_sch and out_date >= cur_date

group by ls) sch,

(select rayon.name rayon,
stat.name stat,
stat_fider.name fider,
tp.name tp,
r .*
from (select substr(r.parents, 1, 3) p1,
substr(r.parents, 4, 3) p2,
substr(r.parents, 7, 3) p3,
substr(r.parents, 10, 3) p4,
substr(r.parents, 1, 6) p5,
substr(r.parents, 1, 9) p6,
r.*
from reg r
where r.del > 0) r,
(select * from reg r where del > 0 and id_reg_type = 1) rayon,
(select * from reg r where del > 0 and id_reg_type = 2) stat,
(select * from reg r where del > 0 and id_reg_type = 3) stat_fider,
(select * from reg r where del > 0 and id_reg_type = 4) tp
where r.p1 = rayon.num(+) and
(r.p1 = stat.parents(+) and r.p2 = stat.num(+)) and
(r.p5 = stat_fider.parents(+) and r.p3 = stat_fider.num(+)) and
(r.p6 = tp.parents(+) and r.p4 = tp.num(+))) r,

(select o.ls,

sum(o.oplata_o + o.oplata_e) osn_sum,
sum(o.opl_peni_o + o.opl_peni_e) peni_sum,
sum(o.opldop_o + o.opldop_e) dop_sum,
sum(o.summa) all_sum,
sum(nvl(od.ndy, 0)) ndy,
sum(nvl(od.podkl, 0)) podkl,
sum(nvl(od.akt, 0)) akt,
sum(nvl(od.svarka, 0)) svarka,
sum(nvl(od.proch, 0)) proch

from ".$arx."oplata o,
(select u.tnid,
u.tn,
o.*,
decode(tnid, 281, o.summa, 0) ndy,
decode(tnid, 43, o.summa, 0) podkl,
decode(tnid, 101, o.summa, 0) akt,
decode(tnid, 47, o.summa, 0) svarka,
decode(tnid, 281, 0, 43, 0, 101, 0, 47, 0, o.summa) proch
from ".$arx."oplata o,
(select tn.name tn, tn.id tnid, u.*
from ".$arx."uslugi u, tariff_name tn
where u.del > 0 and u.id_tariff_code = tn.id and
u.id in
(select o.id_uslugi
from ".$arx."oplata o
where o.del > 0 and o.id_uslugi > 0)) u
where o.del > 0 and o.id_uslugi = u.id) od
where o.del > 0 and o.id = od.id(+)
group by o.ls) o

where c.id_ocrug = $id_ocrug and c.id_diller = r.id and
c.ls = o.ls and c.ls = sch.ls(+)
group by r.id, stat, fider, r.name
order by r.id, stat, fider

";
Добавить сообщение
Чтобы добавлять комментарии зарeгиcтрирyйтeсь