数据库中有两个表,
表1(编号是主键,不可能重复)
编号, 截止时间,……
1      2008-1-1  
2      2008-1-2
3      2008-1-3表2
编号   推迟时间
1      2008-1-6
1      2008-2-1
3      2008-4-1
我现在想查询一下, 时间小于2008-1-8的数据,表2中有的编号,以表2中的推迟时间为准,表2中有重复编号的,以最大的推迟时间为准;表2中没有的,以表1中截止时间为准。以上数据查询的结果应为
编号      截止时间     推迟时间
2         2008-1-2       空
如何写sql语句?

解决方案 »

  1.   

    if object_id('[tb1]') is not null drop table [tb1] 
     go 
    create table [tb1]([编号] int,[截止时间] datetime)
    insert [tb1] select 1,'2008-1-1'
    union all select 2,'2008-1-2'
    union all select 3,'2008-1-3'
    go
    if object_id('[tb2]') is not null drop table [tb2] 
     go 
    create table [tb2]([编号] int,[推迟时间] datetime)
    insert [tb2] select 1,'2008-1-6'
    union all select 1,'2008-2-1'
    union all select 3,'2008-4-1'
    go
    select a.*,b.推迟时间
    from tb1 a
      left join (select 编号,MAX([推迟时间]) [推迟时间] from tb2 group by 编号) b
        on a.编号=b.编号
    where ISNULL(b.推迟时间,a.截止时间)<'2008-1-8'
    /*
    编号          截止时间                    推迟时间
    ----------- ----------------------- -----------------------
    2           2008-01-02 00:00:00.000 NULL(1 行受影响)
    */
      

  2.   

    create table A(编号 int,截止时间 datetime)
    insert into A select  
    1,'2008-1-1' union all select
    2,'2008-1-2' union all select
    3,'2008-1-3' create table B(编号 int,推迟时间 datetime)
    insert into B select 
    1,'2008-1-6' union all select
    1,'2008-2-1' union all select
    3,'2008-4-1' 
    select A.编号,截止时间,推迟时间=isnull(convert(varchar(20),推迟时间,120),'空') from A left join B on A.编号=B.编号
    where not exists (select 1 from B C where B.编号=C.编号 and B.推迟时间<C.推迟时间) 
    /*
    编号          截止时间                                                   推迟时间                 
    ----------- ------------------------------------------------------ -------------------- 
    1           2008-01-01 00:00:00.000                                2008-02-01 00:00:00
    2           2008-01-02 00:00:00.000                                空
    3           2008-01-03 00:00:00.000                                2008-04-01 00:00:00(所影响的行数为 3 行)
    */drop table A,B
      

  3.   

    select A.编号,截止时间,推迟时间=isnull(convert(varchar(20),推迟时间,120),'空') from A left join B on A.编号=B.编号
    where not exists (select 1 from B C where B.编号=C.编号 and B.推迟时间<C.推迟时间) and ISNULL(b.推迟时间,a.截止时间)<'2008-1-8'
      

  4.   

    --把楼上的改一小下:
    if object_id('[tb1]') is not null drop table [tb1] 
     go 
    create table [tb1]([编号] int,[截止时间] datetime)
    insert [tb1] select 1,'2008-1-1'
    union all select 2,'2008-1-2'
    union all select 3,'2008-1-3'
    go
    if object_id('[tb2]') is not null drop table [tb2] 
     go 
    create table [tb2]([编号] int,[推迟时间] datetime)
    insert [tb2] select 1,'2008-1-6'
    union all select 1,'2008-2-1'
    union all select 3,'2008-4-1'
    select a.编号,convert(varchar(10),a.截止时间,120) as 截止时间 ,b.推迟时间
    from tb1 a
      left join (select 编号,MAX([推迟时间]) [推迟时间] from tb2 group by 编号) b
        on a.编号=b.编号
    where ISNULL(b.推迟时间,a.截止时间)<'2008-1-8'
    /*
    编号          截止时间                    推迟时间
    ----------- ----------------------- -----------------------
    2           2008-01-02                 NULL(1 行受影响)
    */