日期:2014-05-17 浏览次数:20817 次
select
任务号,
(select top 1 时间 from tb b where a.任务号=b.任务号 order by 时间 asc) as 第一次称重时间,
(select top 1 时间 from tb c where a.任务号=c.任务号 order by 时间 desc) as 最后一次称重时间
from
tb a
if OBJECT_ID('table1','u') is not null
drop table table1
go
create table table1
(
CarNo int,
MissionNo int,
Heavy int,
MiTime datetime
)
go
insert into table1 values
(1001,20001,16,'2012-10-15 12:38:55'),
(1002,20001,2,'2012-10-16 20:38:55'),
(1003,20001,12,'2012-10-17 22:38:55'),
(1004,20002,11,'2012-12-15 22:30:55'),
(1005,20002,22,'2012-12-16 22:38:55'),
(1006,20003,9,'2012-12-17 22:38:55')
--SQL
--第一次
select top 1 MiTime From table1 where MissionNo=20001 order by MiTime
--最后一次
select top 1 MiTime From table1 where MissionNo=20001 order by MiTime desc