我需要这个小组成员的帮助。我在两个表中比较计划
这是一个测试用例
create table t11
(
sql_id varchar2(20),
id number,
plan_hash_value number,
OPERATION varchar2(300),
options varchar2(30),
OBJECT_NAME varchar2(30),
cost number
);
create table t22
(
sql_id varchar2(20),
id number,
plan_hash_value number,
OPERATION varchar2(300),
options varchar2(30),
OBJECT_NAME varchar2(30),
cost number
);
insert into t11 values('00znbpauyyn1m',0,3587696061,'SELECT STATEMENT',null,null,10);
insert into t11 values('00znbpauyyn1m',1,3587696061,'TABLE ACCESS FULL',null,'TABLE_1',1);
insert into t22 values('00znbpauyyn1m',0,3587696061,'SELECT STATEMENT',null,null,10);
insert into t22 values('00znbpauyyn1m',1,3587696061,'TABLE ACCESS FULL',null,'TABLE_1',1);
insert into t11 values('grfrdz59pu6mc',0,60452177,'SELECT STATEMENT',null,null,5);
insert into t11 values('grfrdz59pu6mc',1,60452177,'INDEX UNIQUE SCAN',null,'TABLE_fry',1);
commit;
我尝试了什么
select distinct a.sql_id,case when a.Plan_hash_value=b.Plan_hash_value then 'plan same' when b.Plan_hash_value is null then 'plan not found in table t22' else 'plan changed' end as status
,(select nvl(to_char(cost),'n/a') from t11 a where a.id=0 and a.sql_id=b.sql_id)cost_t11,
(select nvl(to_char(cost),'n/a') from t22 b where b.id=0 and a.sql_id=b.sql_id)cost_t22
from t11 a left outer join t22 b
on a.sql_id=b.sql_id;
输出
SQL_ID STATUS cost_t11 cost_t22
grfrdz59pu6mc plan not found in table t22
00znbpauyyn1m plan same 10 10
我在两个表中比较计划,上面的查询给出了我所需的结果
这里的
sql_id=grfrdz59pu6mc is only present in table t11 not in t22
所以当我使用上面的查询时sql_id=grfrdz59pu6mc in table t11 is not displayed.
我希望t11
中sql_id=grfrdz59pu6mc and n/a(handle null)
的{{1}}表显示费用
答案 0 :(得分:0)
我认为您可以在full outer join
子句中使用select
和一些条件:
select coalesce(a.id, b.id) as id,
(case when a.plan_hash_value is null then 'Plan not found in table t11'
when b.plan_hash_value is null then 'Plan not found in table t22'
else 'plans same'
end) as status,
a.cost as cost_t11, b.cost as cost_t22
from t11 a full outer join
t22 b
on a.id = b.id;
答案 1 :(得分:0)
我正在使用Tom Kyte提出的那个:
SELECT MAX (tab) tgt,
rn,
A,
B,
C
FROM ( (SELECT 'SRC' tab,
ROWNUM rn,
A,
B,
C
FROM TA)
UNION ALL
(SELECT 'DST' tab,
ROWNUM rn,
A,
B,
C
FROM TB))
GROUP BY rn,
A,
B,
C
HAVING COUNT (*) != 2
ORDER BY rn ASC, MAX (tab) DESC
作品是一个魅力:)