有两个表TABLEA和TABLEB
tableA addr name
0001 aa
0002 bb
0003 cctableB
id taddr scorse datetime
1  0001   60     2007
2  0001   70     2003 
3  0001   70     2004
4  0002   80     2008
5  0002   90     2009
6  0003   65     2001
7  0003   78     2034
8  0004   56     2007要求得到 tableC
addr scorse datetime
0001   70     2004
0002   90     2009
0003   78     2034如上,希望在tableB中查找tableA中所有的addr对应的唯一一个scorse和datatime.谁能解决,谢谢?

解决方案 »

  1.   

    select a.*,b.* --楼主可以自定义显示列
    from tableA a join tableB b on a.addr=b.taddr
      

  2.   

    select max(b.id),b.addr,b.scorse,b.datetime from tableA as a,tableB as b where a.addr = b.taddr group by b.taddr
      

  3.   


    create table tableA(addr varchar(10),[name] varchar(10))
    insert tableA select '0001','bb'
    union all select '0002', 'bb'
    union all select '0003', 'cc'
    gocreate table tableB(id int identity(1,1),taddr varchar(10),scorse int,datetime varchar(10))
    insert tableB select '0001',   60 ,    '2007'
    union all select '0001',   70 ,    '2003'
    union all select '0001',   70 ,    '2004'
    union all select '0002',   80 ,    '2008'
    union all select '0002',   90 ,    '2009'
    union all select '0003',   65 ,    '2001'
    union all select '0003',   78 ,    '2034'
    union all select '0004',   56 ,    '2007'go
    select taddr,max(scorse) as scorse,max(datetime)as [datetime] 
    from tableB where taddr in(select addr from tablea)group by taddr----------------------
    taddr   scorse  datetime
    0001 70 2007
    0002 90 2009
    0003 78 2034
      

  4.   

    select taddr,max(scorse) as scorse,max(datetime)as [datetime] 
    into tableC
    from tableB where taddr in(select addr from tablea)group by taddr
      

  5.   

    roy_88(中国风_燃烧你的激情!!!) 
    谢谢,你的办法不能排除重复项。dg4134(阿牛) ( )
    服务器: 消息 8120,级别 16,状态 1,行 1
    列 'b.temperature' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    服务器: 消息 8120,级别 16,状态 1,行 1
    列 'b.Collect_Time' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
      

  6.   

    我晕~~我加班给你做题目
    select * from tableB 
    where id in(select max(id)as 'hid' from tableB 
    where taddr in(select taddr from tableA )group by taddr)