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

求教一个SQL语句
Car_No         AppGoTime       AppBackTime                                                                                        
--------------------   ------------------------------------------------
晋AW00001           2007-08-21   18:38:00.000           2007-09-21   18:38:00.000
晋AW00004           2007-07-21   18:41:00.000           2007-10-21   18:41:00.000
晋AW00001           2007-05-21   18:40:00.000           2007-05-24   18:40:00.000
晋AW00001           2007-04-23   17:31:00.000           2007-04-23   17:32:00.000
晋AW00010           2007-04-23   17:30:00.000           2007-04-23   18:50:00.000
晋AW00001           2007-04-23   17:14:00.000           2007-04-23   23:14:00.000
晋AW00001           2007-04-21   18:44:00.000           2007-04-22   21:44:00.000
晋AW00001           2007-04-21   18:42:00.000           2007-04-22   18:42:00.000
晋AW00010           2007-04-21   18:31:00.000           2007-04-22   10:31:00.000
晋AW00010           2007-04-20   13:58:00.000           2007-04-21   13:58:00.000
晋AW00004           2007-04-20   11:26:00.000           2007-04-21   11:26:00.000
晋AW00001           2007-04-20   11:03:00.000           2007-04-20   17:03:00.000

数据如上所示,现在求一个SQL语句查询,返回的结果是每一个Car_No返回一条记录,这条记录是AppGoTime字段值最大的,请高手指点。

------解决方案--------------------
select car_no,max(AppGoTime) AppGoTime,max(AppBackTime) AppBackTime
from train group by Car_No
因为对一辆车来说出去晚回来也晚
------解决方案--------------------
select car_no,AppGoTime,AppBackTime from train
where AppGoTime=(select max(AppGoTime) from train b where b.car_no=train.car_no)
还有未回的情况未考虑