有一个库,里面的:A 表 是 企业用户的基本信息:SID      NAME
--------------
  2      AAAA
  5      BB
--------------B 表 是上面 A 表里的企业交的数据,如下:
SID      DATETIME      NOTES
------------------------------
 2       1998-3-4        XXXX
 5       2003-1-3        BBBB
 2       2005-8-9        CCCC
 2       2005-7-3        CCCC
------------------------------
请问:1 : 我要找出 A 表里, 2005年7月未交数据的企业应如何找?2 : 我要找 BB 这个企业最后递交的数据又如何?

解决方案 »

  1.   

    1.
    select * from A where SID not in
    (
    select SID from B where DATETIME between '2006-7-1' and '2006-7-31'
    )
      

  2.   

    1. select * from tbA as a where not exists(
           Select * from tbB where SID=a.SID and Datediff(month,DATETIME,'2005-07-01')=0)2. Select b.* from tbA as a,tbB as b
       where a.NAME='BB' and b.SID=a.SID and not exists(
                select * from tbB where SID=b.SID and DATETIME>b.DATETIME)
      

  3.   

    2.SELECT * FROM B b1 WHERE not exists 
    (
    SELECT 1 FROM B WHERE DATETIME<b1.DATETIME and SID=b1.SID
    )
      

  4.   

    1:
    select * from A where sid not in(select sid from B where left([datetime],6)='2005-07')
    2:
    select top 1 B.* from B,A where A.sid=B.sid and A.[name]='BB' order by B.[datetime] desc
      

  5.   

    1.select name from a where sid not in(select sid from b where convert(char(6),[datetime],112)='200507')2.select * from a inner join b on a.sid =b.sid where name='BB' and [datetime]=(select max([datetime]) from b where name='BB')
      

  6.   

    1:
    select * from A where sid not in(select sid from B where Datediff(month,[DATETIME],'2005-07-01')=0)
      

  7.   

    1。select * from 表A where sid  not in (select 表A.sid from 表A left join 表B on 表A.sid=表B.sid where datepart(month,stimg)='7'and datepart(year,stimg)='2005')
      

  8.   

    1、SELECT * 
    FROM A 
    WHERE sid not in 
    (SELECT sid 
    FROM b 
    WHERE 
    --Datediff(month,[DATETIME],'2005-07-01')=0)
    [datetime] between '2005-07-01' and '2005-07-31')2、
    SELECT TOP 1 b.* FROM b,a
    WHERE b.sid=a.sid
    AND a.[name]='bb'
    ORDER BY b.[datetime] DESC