最近三个月都在做报表开发,就牵涉到sql了
有个问题:
用Oracle,
两个含有group by 的数据集
第一个数据集为空,第二个数据集有值
再full join 后,数据就对不上了,
查询出来的值是两个数据集的约束条件都会丢失的笛卡尔集.
sql:
select aa.pack_sign_provice,
aa.pack_sign_city,
bb.pack_sign_provice,
bb.pack_sign_city,
aa.dq_cnt,
bb.cq_cnt
from (select pack_sign_provice, pack_sign_city, count(1) dq_cnt
from itl_tz_pact a
where a.orgid in
(select b.orgid
from om_organization b
start with b.orgid = 127
connect by prior b.orgid = b.parentorgid)
and a.STARTTIME < sysdate
and a.ENDTIME < (7 + sysdate)
and a.ENDTIME > sysdate
group by pack_sign_provice, pack_sign_city)/*第一个数据集*/ aa
full join (select pack_sign_provice, pack_sign_city, count(1) as cq_cnt
from itl_tz_pact a
where a.orgid in
(select b.orgid
from om_organization b
start with b.orgid = 127
connect by prior b.orgid = b.parentorgid)
and a.ENDTIME < sysdate
group by pack_sign_provice, pack_sign_city)/*第二个数据集*/ bb
on aa.pack_sign_provice = bb.pack_sign_provice
and aa.pack_sign_city = bb.pack_sign_city;
看解析计划:
费解.第一个数据集里有个filter.在最后又出现一个filter.
若把两个数据集对调,则不出现问题.
若把上面两个查询 create 成表,
解析为:
郁闷...
电话给代理工程师,说不可能出现这样的问题.....更郁闷.............
求大虾们 说明.......解释..............