select pcode from (
select h.k,h.d,h.m,h.u,l.areacode,
l.areacode||substr(h.u,4,length(h.u))||h.k pcode
from(
select j.k,j.d,j.m,j.u from(
select lpad(f,3,0) as k,d, m,u from(
select xx.areacode d,nvl(count(cy.pcode),'001')f,mx.m,mx.shopcode u
from cz_s_jtxx xx, cz_s_jtcy cy ,
( select a.m,d.shopid,d.shopcode from
(select shopid m,parentid from s_shop where shoptype=1) a,
(select shopid,parentid from s_shop where shoptype=2) b,
(select shopid,parentid from s_shop where shoptype=3)c,
(select shopid,parentid,shopcode from s_shop where shoptype=4)d
where d.parentid=c.shopid
and c.parentid=b.shopid
and b.parentid=a.m
and d.shopid=14725) mx
where xx.pcode = cy.pcode
and xx.areacode=mx.shopid
group by xx.areacode,mx.m,mx.shopcode))j)h,
(select areacode,shopid from cz_s_area) l
where h.m=l.shopid)
首先 从最里层的 sql说起:
select a.m,d.shopid,d.shopcode from
(select shopid m,parentid from s_shop where shoptype=1) a,
(select shopid,parentid from s_shop where shoptype=2) b,
(select shopid,parentid from s_shop where shoptype=3)c,
(select shopid,parentid,shopcode from s_shop where shoptype=4)d
where d.parentid=c.shopid
and c.parentid=b.shopid
and b.parentid=a.m
and d.shopid=14725
这条sql能让我们根据机构找到最上级的shopid,和shopcode 当然这种写法不是唯一的我们可以用oracle的
start with ...connect by prior 函数来处理这个
不过我这里没采用,当有了这个以后我么能查出一条唯一的条目,根据这个条目我们可以把他看作一个表作为子表来处理
select lpad(f,3,0) as k,d, m,u from(
select xx.areacode d,nvl(count(cy.pcode),'001')f,mx.m,mx.shopcode u
from cz_s_jtxx xx, cz_s_jtcy cy ,
( select a.m,d.shopid,d.shopcode from
(select shopid m,parentid from s_shop where shoptype=1) a,
(select shopid,parentid from s_shop where shoptype=2) b,
(select shopid,parentid from s_shop where shoptype=3)c,
(select shopid,parentid,shopcode from s_shop where shoptype=4)d
where d.parentid=c.shopid
and c.parentid=b.shopid
and b.parentid=a.m
and d.shopid=14725) mx
where xx.pcode = cy.pcode
and xx.areacode=mx.shopid
group by xx.areacode,mx.m,mx.shopcode)
通过这个等值链接我们可以确定上一个步骤所对应出的家庭xx表的对应条目这时