一个表里导入了两批数据,根据batch这个字段来确定他们的批次。
我现在想通过c1和c2这两个字段比较确定这个表里发生了变化的数据记录。
两批数据通过c1这个字段关联,根据c2字段的变化来确定这条记录是否变化。
每批数据c1有重复值,我通过下面的语句能找出两批数据相同的记录:
select * from Table where c1 in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2<>b.c2 )
)
这个结果是正确的;但我改成下面的语句(把in改成not in)想查询不同的记录:
select * from Table where c1 not in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2<>b.c2 )
)
结果总是出不来请问这个问题改怎么解决?在下不胜感激了!
我现在想通过c1和c2这两个字段比较确定这个表里发生了变化的数据记录。
两批数据通过c1这个字段关联,根据c2字段的变化来确定这条记录是否变化。
每批数据c1有重复值,我通过下面的语句能找出两批数据相同的记录:
select * from Table where c1 in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2<>b.c2 )
)
这个结果是正确的;但我改成下面的语句(把in改成not in)想查询不同的记录:
select * from Table where c1 not in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2<>b.c2 )
)
结果总是出不来请问这个问题改怎么解决?在下不胜感激了!
解决方案 »
- orcle 10G的问题
- 挑战高难度sql语句~~一句sql可行吗?
- 关于Oracle SQL Developer的菜鸟问题
- sqlserver2005中的一个库移植到oracle 10g求详细步骤 急用
- 各位高人请指点,哪有oracle的视频?谁的比较好?能不能告诉一下网址?
- 使用Oracle 数据库链的SQL语句 与不使用 数据库链的SQL语句两者有什么区别 ??
- 10G RAC中 "GSD" "Listener" "ONS daemon"三个app是起什么作用的?
- sqlite3怎么样自定义一个int64位的类型,大家帮帮忙吧??????????
- 请各位赐教:在哪儿可以下载到免费的如下软件?
- 基于B/S结构(java+weblogic+oracle)如果事物处理比较好?分不够会一直往上加!
- 超简单问题:set pause的用法
- Oracle9i 无法登陆问题?急!
有时候加了distinct 返回的值也不一定就是唯一的,如果是这样的话,试着用rowid为条件试一试
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2 <> b.c2 )
出来的结果select * from Table where c1 not in (上面的结果集);
你就发现你写的sql不是你想要的结果了
如果后面的数据量很大,用not in查询起来就很慢
怎么换个语句来解决这个问题?
select * from Table where c1 in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2 <> b.c2 )
) 第二个要求:
select * from Table where c1 in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2 = b.c2 )
)
〉select * from Table where c1 in
〉(
〉 select distinct a.c1 from
〉 (select distinct c1,c2 from t_Table where batch='1' ) a,
〉 (select distinct c1,c2 from t_Table where batch='2' ) b
〉 where (a.c1=b.c1 and a.c2 <> b.c2 )
〉)
这个时候,你得到的是两批数据相同的数据?
你的batch1 和 batch2里面,是不是存在同样的c1,但是出现多条C2,
batch1和batch2的c2之间,既有相同的,又有不同的?我看你好像迷惑的是这里。那这个时候,你又想要什么呢?
不好意思,是我把SQL搞反了
我的意思是这样
select * from Table where c1 not in
(
select distinct a.c1 from
(select distinct c1,c2 from t_Table where batch='1' ) a,
(select distinct c1,c2 from t_Table where batch='2' ) b
where (a.c1=b.c1 and a.c2 = b.c2 )
)
找出不同的数据的话很难查出来
a.c2 = b.c2
batch1和batch2的c2之间,既有相同的,又有不同的? 给出一部分数据看看呢?