W
wpw72
Unregistered / Unconfirmed
GUEST, unregistred user!
有2个ms-sqlserver的table,一个Device如下<br>ID Name Model<br>1 aa dsd<br>2 df sd<br>3 ds dss<br>4 dds er<br>另一个table 名ServRec如下<br>ID DeviceID ServDate SupplierID<br>1 1 2007/1/1 2<br>2 1 2006/1/2 3<br>3 2 2005/2/3 1<br>4 2 2007/1/3 4<br>5 2 2008/1/4 1<br>希望查询后出现<br>DeviceID Name Model ServDate SupplierID<br> 1 aa dsd 2007/1/1 2<br> 2 df sd 2008/1/4 1<br> 3 ds dss null null<br> 4 dds er null null<br><br>我想选择Device表中所有记录,然后左连接ServRec中ServDate为最新的那条记录<br>SELECT MAX(ServRec.ServDate) AS ServDate, ServRec.SupplierID, Device.Name, <br> Device.Model,Device.ID AS DeviceID<br>FROM Device LEFT OUTER JOIN<br> ServRec ON Device.ID = ServRec.DeviceID<br>GROUP BY ServRec.SupplierID, Device.Name, Device.Model,Device.ID<br>出现结果<br>DeviceID Name Model ServDate SupplierID<br> 1 aa dsd 2007/1/1 2<br> 1 aa dsd 2006/1/2 3<br> 2 df sd 2007/1/3 4<br> 2 df sd 2008/1/4 1<br> 3 ds dss null null<br> 4 dds er null null<br>怎样才能得到我要的结果?