有两个表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.谁能解决,谢谢?
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.谁能解决,谢谢?
from tableA a join tableB b on a.addr=b.taddr
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
into tableC
from tableB where taddr in(select addr from tablea)group by taddr
谢谢,你的办法不能排除重复项。dg4134(阿牛) ( )
服务器: 消息 8120,级别 16,状态 1,行 1
列 'b.temperature' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
服务器: 消息 8120,级别 16,状态 1,行 1
列 'b.Collect_Time' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
select * from tableB
where id in(select max(id)as 'hid' from tableB
where taddr in(select taddr from tableA )group by taddr)