大家先不要管这两条查询语句的业务逻辑是什么,就看这两条语句为什么会返回不同的结果?
两条语句对应的exists完全相同,在我看来这两条语句是等价的呀。
--创建测试环境
create table #t1(编号 int,部门 varchar(10),日期1 datetime,状态 bit)
create table #t2(编号 int,部门 varchar(10),日期2 datetime,状态 bit)
create table #t3(编号 int,部门 varchar(10),日期 datetime)--插入测试数据
insert #t1(编号,部门,日期1,状态)
select '10001','01','2007-02-02','0' union all
select '10002','01','2007-02-06','0' union all
select '10003','02','2007-02-01','0' union all
select '10006','02','2007-03-02','0' union all
select '10001','03','2007-03-01','0' union all
select '10005','03','2007-03-01','0'insert #t2(编号,部门,日期2,状态)
select '10003','02','2007-02-10','1'insert #t3(编号,部门,日期)
select '10001','01','2007-02-03' union all
select '10002','01','2007-02-10' union all
select '10003','02','2007-02-06' union all
select '10006','02','2007-03-02' union all
select '10001','03','2007-03-01' union all
select '10005','03','2007-02-22' union all
select '10005','03','2007-03-02'--求解过程1
select *
from #t3 t3
where exists(
      select 1 
      from #t1 t1 
      where not exists--如果表1中有2个以上相同编号则以日期1最大的为条件 
                  (select 1 from #t1 where 编号 = t1.编号 and 日期1 > t1.日期1)
            and t3.编号 = t1.编号 and t3.部门 = t1.部门--表3中的编号和部门同时存在表1中
            and not exists--表3中的编号和部门同时表2中不存在
                  (select 1 from #t2 where 编号 = t1.编号 and 部门 = t1.部门)
      )
      
union allselect *
from #t3 t3
where exists(
      select 1 
      from #t1 t1 
      where not exists--如果表1中有2个以上相同编号则以日期1最大的为条件
                  (select 1 from #t1 where 编号 = t1.编号 and 日期1 > t1.日期1)
            and t3.编号 = t1.编号 and t3.部门 = t1.部门--表3中的编号和部门同时存在表1中
            and exists--表3中的编号和部门同时存在表2中
                  (select 1 from #t2 where 编号 = t1.编号 and 部门 = t1.部门)
            and not exists--当表1.max(日期1)>表2.max(日期2)才满足条件
                  (select 1 from #t2 where 编号 = t1.编号 and 部门 = t1.部门 and 日期2 >= t1.日期1)
      )
/*--测试结果
编号          部门         日期                                                     
----------- ---------- -------------------------
10002       01         2007-02-10 00:00:00.000
10006       02         2007-03-02 00:00:00.000
10001       03         2007-03-01 00:00:00.000
10005       03         2007-02-22 00:00:00.000
10005       03         2007-03-02 00:00:00.000(所影响的行数为 5 行)
*/-------------------------------------------------------------------------------求解过程2
select *
from #t3 t3
where exists(
      select 1 
      from #t1 t1 
      where not exists--如果表1中有2个以上相同编号则以日期1最大的为条件 
                  (select 1 from #t1 where 编号 = t1.编号 and 日期1 > t1.日期1)
            and t3.编号 = t1.编号 and t3.部门 = t1.部门--表3中的编号和部门同时存在表1中
            and not exists--表3中的编号和部门同时表2中不存在
                  (select 1 from #t2 where 编号 = t1.编号 and 部门 = t1.部门)
      )
      or exists(
      select 1 
      from #t1 t1 
      where not exists--如果表1中有2个以上相同编号则以日期1最大的为条件
                  (select 1 from #t1 where 编号 = t1.编号 and 日期1 > t1.日期1)
            and t3.编号 = t1.编号 and t3.部门 = t1.部门--表3中的编号和部门同时存在表1中
            and exists--表3中的编号和部门同时存在表2中
                  (select 1 from #t2 where 编号 = t1.编号 and 部门 = t1.部门)
            and not exists--当表1.max(日期1)>表2.max(日期2)才满足条件
                  (select 1 from #t2 where 编号 = t1.编号 and 部门 = t1.部门 and 日期2 >= t1.日期1)
      )
            
--删除测试环境
drop table #t1,#t2,#t3/*--测试结果
编号          部门         日期                                                     
----------- ---------- -----------------------------
10002       01         2007-02-10 00:00:00.000
10003       02         2007-02-06 00:00:00.000
10006       02         2007-03-02 00:00:00.000
10001       03         2007-03-01 00:00:00.000
10005       03         2007-02-22 00:00:00.000
10005       03         2007-03-02 00:00:00.000(所影响的行数为 6 行)
*/      

解决方案 »

  1.   

    逻辑太乱?这样写还乱吗?select * from t3
    where exists(A) or exists(B)与select * from t3 where exists(A) 
    union all
    select * from t3 where exists(B) 是不是一个意思?另,业务逻辑:按部门汇总统计表3中不同的编号数(即不重复统计相同的编号),条件是:
    一。
    1》表3中的编号和部门同时存在表1中(表3.编号=表1.编号and 表3.部门=表1.部门),并且在表2中不存在,
    2》如果表1中有2个以上记录满足条件1,则以日期1最大的为条件,如编号为10001的则以,编号=10001、部门=03为条件,故在表3中不统计编号=10001、部门=01的;二。或者
    表3中的编号和部门同时存在表1和表2中,则当表1.max(日期1)>表2.max(日期2)才满足条件。如编号为10003,同时存存表1和表2中,但由于表1.max(日期1)<表2.max(日期2)(2007-02-01<2007-02-10)故在表3中不统计这条记录。 原帖见
    http://community.csdn.net/Expert/topic/5373/5373716.xml?temp=.6307947
      

  2.   

    zheninchangjiang(徐若涵) 
    你的结果是一致的?Microsoft SQL Server  2000 - 8.00.194 (Intel X86) 
    Aug  6 2000 00:57:48 
    Copyright (c) 1988-2000 Microsoft Corporation
    Personal Edition on Windows NT 5.1 (Build 2600: Service Pack 2)有问题吗?盗版的。操作系统是番茄花园XP。
      

  3.   

    select * from t3
    where exists(A) or exists(B)与select * from t3 where exists(A) 
    union all
    select * from t3 where exists(B) 条件exists(A) 与 exists(B)是互斥的,所以union all后不会出现重复的。
    那么这两条语句结果就应该是一样的。 
     
      

  4.   

    没有什么难的  就是你的显示方法重写了  第二次的那个显示方法  把你or前面的判断给重写了
    你把最后的>=变成<=可能会更好理解
      

  5.   

    受楼上朋友的的提醒,找到原因了。
    select * from t3 where exists(A) or exists(B)
    这两个exists可以合并成一个exists。
    出错的原因应该是查询分析器在将它们合并的过程中优化错了。
    --合并成一个exists就对了
    --创建测试环境
    create table #t1(编号 int,部门 varchar(10),日期1 datetime,状态 bit)
    create table #t2(编号 int,部门 varchar(10),日期2 datetime,状态 bit)
    create table #t3(编号 int,部门 varchar(10),日期 datetime)--插入测试数据
    insert #t1(编号,部门,日期1,状态)
    select '10001','01','2007-02-02','0' union all
    select '10002','01','2007-02-06','0' union all
    select '10003','02','2007-02-01','0' union all
    select '10006','02','2007-03-02','0' union all
    select '10001','03','2007-03-01','0' union all
    select '10005','03','2007-03-01','0'insert #t2(编号,部门,日期2,状态)
    select '10003','02','2007-02-10','1'insert #t3(编号,部门,日期)
    select '10001','01','2007-02-03' union all
    select '10002','01','2007-02-10' union all
    select '10003','02','2007-02-06' union all
    select '10006','02','2007-03-02' union all
    select '10001','03','2007-03-01' union all
    select '10005','03','2007-02-22' union all
    select '10005','03','2007-03-02'--求解过程
    select *
    from #t3 t3
    where exists(
                select 1 
                from #t1 t1 
                where not exists--如果表1中有2个以上相同编号则以日期1最大的为条件 
                            (select 1 from #t1 where 编号 = t1.编号 and 日期1 > t1.日期1)
                      and t3.编号 = t1.编号 and t3.部门 = t1.部门--表3中的编号和部门同时存在表1中
                      and (
                            not exists--表3中的编号和部门同时表2中不存在
                                  (select 1 from #t2 where 编号 = t1.编号 and 部门 = t1.部门)
                            or exists--表3中的编号和部门同时存在表2中
                                  (select 1 from #t2 where 编号 = t1.编号 and 部门 = t1.部门)
                            and not exists--当表1.max(日期1)>表2.max(日期2)才满足条件
                                  (select 1 from #t2 where 编号 = t1.编号 
                                        and 部门 = t1.部门 and t1.日期1 <= 日期2)
                      )
          )
          
                
    --删除测试环境
    drop table #t1,#t2,#t3/*--测试结果
    编号          部门         日期                                                     
    ----------- ---------- -------------------------
    10002       01         2007-02-10 00:00:00.000
    10006       02         2007-03-02 00:00:00.000
    10001       03         2007-03-01 00:00:00.000
    10005       03         2007-02-22 00:00:00.000
    10005       03         2007-03-02 00:00:00.000(所影响的行数为 5 行)
    */
      

  6.   

    我的sql server没打补丁,呵呵。
      

  7.   

    这个和补丁有关系么?  最原始的逻辑问题不是么
    mygod  楼主的代码重写了以前的判断俄  这样也要修改阿 踹比尔一脚...
      

  8.   

    select * from t3
    where exists(A) or exists(B)与select * from t3 where exists(A) 
    union all
    select * from t3 where exists(B) 是不是一个意思?------------------
    不是,跟 
    select * from t3 where exists(A) 
    union 
    select * from t3 where exists(B) 
    一个意思
      

  9.   

    你的什么SQL呀,我的查出来就一样
      

  10.   

    借个地方,我怎么发贴?在哪里进入发贴界面?
    有谁能够提供SQL SERVER 安装服务或支持?有偿服务.
    13951709072
      

  11.   

    在SP3,SP4下测试,查询结果安全一样。
      

  12.   

    to:wangdehao(找找找(现在很幸福)) ( ) 我已解释过啦
    mengmou()mengmou() ( ) 信誉:100    Blog  2007-3-3 16:32:45  得分: 0    
    select * from t3
    where exists(A) or exists(B)与select * from t3 where exists(A) 
    union all
    select * from t3 where exists(B) 条件exists(A) 与 exists(B)是互斥的,所以union all后不会出现重复的。
    那么这两条语句结果就应该是一样的。 
    Microsoft SQL Server  2000 - 8.00.194 (Intel X86) 
    Aug  6 2000 00:57:48 
    Copyright (c) 1988-2000 Microsoft Corporation
    Personal Edition on Windows NT 5.1 (Build 2600: Service Pack 2)盗版的。还是 Personal sp2,操作系统是番茄花园XP。