我有以下数据
我想比较这些数据。所以,我使用这段代码:
drop view sms4_table;
create view sms4_table as
select snumber,cast(to_char(cdrdate,'YYYYMMDDHH24MISS')as varchar2(14))as cdrdate
from database1@db
where snumber='123456789101098'
and (cdrdate between to_date ('05/07/2014 00:00:00','DD/MM/YYYY HH24:MI:SS')
and to_date ('05/07/2014 23:59:59','DD/MM/YYYY HH24:MI:SS'));
drop view sms3_table;
create view sms3_table as
select cast (substr(snumber,1,15) as varchar2(15)) as snumber,timestamp as answertime
FROM database2
where snumber='123456789101098A';
and timestamp like '20140705%'
select sms4_table.snumber, sms4_table.cdrdate, sms3_table.snumber, sms3_table.answertime
from sms4_table
left join sms3_table
on sms4_table.cdrdate=sms3_table.answertime
and sms4_table.snumber=sms3_table.snumber
where sms3_table.snumber is null;
我在database1中有 20 条记录,在database2中有 19 条记录。数据库之间的唯一区别是数据库1中的两个记录是重复的(这与snumber 123456789101098和cdrdate 20140705033021)。但是,当我运行上面的脚本时,我没有收到差异。我只收到 -
选择了0行
请你支持我。如何纠正脚本以显示这种差异 - snumber 123456789101098和cdrdate 20140705033021?
感谢您提前帮助我。
答案 0 :(得分:1)
您可以向两个视图添加行号伪列,以便可以区分重复值:
create view sms4_table as
select snumber,
to_char(cdrdate,'YYYYMMDDHH24MISS') as cdrdate,
row_number() over (partition by snumber,
to_char(cdrdate,'YYYYMMDDHH24MISS')) as rn
from database1@db
where snumber='123456789101098'
and cdrdate between to_date ('05/07/2014 00:00:00','DD/MM/YYYY HH24:MI:SS')
and to_date ('05/07/2014 23:59:59','DD/MM/YYYY HH24:MI:SS');
create view sms3_table as
select substr(snumber,1,15) as snumber,
timestamp as answertime,
row_number() over (partition by substr(snumber,1,15), timestamp) as rn
from database2
where snumber='123456789101098A'
and timestamp like '20140705%';
然后在连接中包含rn
伪列;我已将其更改为完全外部联接,因此您可以以任何方式发现问题。
select sms4_table.snumber, sms4_table.cdrdate,
sms3_table.snumber, sms3_table.answertime
from sms4_table
full outer join sms3_table
on sms4_table.cdrdate = sms3_table.answertime
and sms4_table.snumber = sms3_table.snumber
and sms4_table.rn = sms3_table.rn
where sms3_table.snumber is null
or sms4_table.snumber is null;
对于您的重复值sms4_table
现在将具有:
snumber cdrdate rn
--------------- -------------- ---
123456789101098 20140705033021 1
123456789101098 20140705033021 2
而sms3_table
只有:
snumber answertime rn
--------------- -------------- ---
123456789101098 20140705033021 1
如果您只加入前两列,那么视图4中的两个记录都将匹配视图3中的记录 - 正如您的原始查询所做的那样。但是,如果在连接条件中包含rn
,则只有第一行匹配,而rn = 2
的第二行不匹配。
如果这是一次性任务,你真的不需要创建视图,你可以只使用内联视图(子查询)。