日期:2014-05-18  浏览次数:20523 次

求一SQL语句,关于Group by,在线等
表A(codeofdoor,dateandtime,numofenent, sname, scode)
  设备号 时间 事件号 人员姓名 人员代号

例如有如下几条记录
 设备号 时间 事件号 人员姓名 人员代号
  1 2007-10-18 12:00 35 aa 001
  1 2007-10-18 12:08 35 bb 002
  1 2007-10-18 11:50 36 aa 001
  1 2007-10-18 13:30 36 cc 001
  2 2007-10-18 12:30 35 bb 002
  2 2007-10-18 10:08 35 aa 001
  2 2007-10-18 12:05 36 aa 001
  2 2007-10-18 17:01 36 bb 002
我想得到的数据是,每个设备号一条记录(时间是最大的),即要得到下面的数据:
  1 2007-10-18 13:30 36 cc 001
  2 2007-10-18 17:01 36 bb 002
就是上面记录的第四条,和最后一条。
请高手帮忙写一下,该怎么查询才能得到,谢谢,在线等。
  

 

------解决方案--------------------
SQL code

select * from A b where time in (select max(time) from A group by codeofdoor having a.codeofdoor=b.codeofdoor)

------解决方案--------------------
select * from a b where not exists(select 1 from a where 设备号=b.设备号 and 时间>b.时间)
------解决方案--------------------
SQL code
--方法一
select t.* from a t where 时间 = (select max(时间) from a where 设备号 = t.设备号)

--方法二
select m.* from a m,
(select 设备号 , max(时间) 时间 from a group by 设备号) n
where m.设备号 = n.设备号 and m.时间 = n.时间

------解决方案--------------------
select a.* from 表a a inner join (select 设备号,max(时间) from 表a group by 设备号) b on a.设备号=b.设备号 and a.时间=b.时间
------解决方案--------------------
SQL code
create table a(设备号 int,时间 datetime,事件号 int,人员姓名 varchar(10),人员代号 varchar(10)) 
insert into a values(1,'2007-10-18 12:00',35,'aa','001') 
insert into a values(1,'2007-10-18 12:08',35,'bb','002') 
insert into a values(1,'2007-10-18 11:50',36,'aa','001') 
insert into a values(1,'2007-10-18 13:30',36,'cc','001') 
insert into a values(2,'2007-10-18 12:30',35,'bb','002') 
insert into a values(2,'2007-10-18 10:08',35,'aa','001') 
insert into a values(2,'2007-10-18 12:05',36,'aa','001') 
insert into a values(2,'2007-10-18 17:01',36,'bb','002') 
go

--方法一
select t.* from a t where 时间 = (select max(时间) from a where 设备号 = t.设备号)
/*
设备号         时间                                                     事件号         人员姓名       人员代号       
----------- ------------------------------------------------------ ----------- ---------- ---------- 
2           2007-10-18 17:01:00.000                                36          bb         002
1           2007-10-18 13:30:00.000                                36          cc         001

(所影响的行数为 2 行)
*/
--方法二
select m.* from a m,
(select 设备号 , max(时间) 时间 from a group by 设备号) n
where m.设备号 = n.设备号 and m.时间 = n.时间
/*
设备号         时间                                                     事件号         人员姓名       人员代号       
----------- ------------------------------------------------------ ----------- ---------- ---------- 
2           2007-10-18 17:01:00.000                                36          bb         002
1           2007-10-18 13:30:00.000                                36          cc         001

(所影响的行数为 2 行)
*/
--drop table a

------解决方案--------------------
SQL code
select * from tb a where 时间 in(select max(时间) from tb b where a.设备号=b.设备号) order by 设备号

------解决方案--------------------
select * from A where dateandtime in (
select t from (