客户表  客户名称  建档日期
合同表  客户名称  签订合同日期
客户表中有的客户名称  合同表中不一定有
现在想查寻 当超过建档日期30天还没有签订合同的客户资料高手帮忙??

解决方案 »

  1.   

    select * from 客户表 t 
    where not exists(select 1 from 合同表 where 客户名称=t.客户名称)
    and datediff(day,建档日期 ,getdate())>30
      

  2.   

    SELECT A.* 
    FROM 客户表
     A LEFT JOIN 合同表 B 
    WHERE B.客户名称=A.客户名称 
    AND DATEDIFF(DD,建档日期,签订合同日期)>30
      

  3.   

    --猜测的
    select
     * 
    from
     客户表
    where 
     客户名称 
    not in
     (select 客户名称 from 合同表)
    and
     datediff(dd,建档日期,getdate())>=30
      

  4.   

    select t.* from 客户表 t where 客户名称 not in 
    (select m.客户名称 from 客户表 m ,合同表 n where m.客户名称 = n.客户名称 and datediff(m.建档日期,签订合同日期) <= 30 )select t.* from 客户表 t where 客户名称 not in (select 客户名称 from 合同表)
      

  5.   

    SQL77
    为什么会提示语错误啊/?
      

  6.   

    SELECT A.* 
    FROM 客户表
     A LEFT JOIN 合同表 B 
    on B.客户名称=A.客户名称 
    AND DATEDIFF(DD,建档日期,签订合同日期)>30
      

  7.   

    --这样呢?
    SELECT
      A.* 
    FROM
     客户表 A LEFT JOIN 合同表 B 
    WHERE
      B.客户名称=A.客户名称 
    AND 
      DATEDIFF(DD,a.建档日期,b.签订合同日期)>30
      

  8.   

    --> 测试数据:@table1
    declare @table1 table([客户名称] varchar(3),[建档日期] varchar(10))
    insert @table1
    select '001','2009-10-01' union all
    select '002','2009-09-01' union all
    select '003','2009-10-02' union all
    select '004','2009-11-11'--> 测试数据:@table2
    declare @table2 table([客户名称] varchar(3),[签订合同日期] varchar(10))
    insert @table2
    select '001','2009-10-30' union all
    select '002','2009-11-01'select * from @table1
    where 客户名称 not in (select 客户名称 from @table2)
    and datediff(day,建档日期,getdate()) > 30 
    --结果
    003 2009-10-02