我有以下查询,但是 accountoneway 表中可以有几行除了成本之外具有相同的数据,但是还有一个 date_created 列。
我需要做的是返回每个子集具有最大 date_created 的行
SELECT outlocation.LocationName as [Out Location]
,inlocation.LocationName as [In Location]
,cast(SUBSTRING(AccountOneWays.startdate,7,2)as char(2)) +'/'+
cast(SUBSTRING(AccountOneWays.startdate,5,2)as char(2))+'/'+
cast(SUBSTRING(AccountOneWays.startdate,1,4)as char(4)) as [Start Date]
,cast(SUBSTRING(AccountOneWays.enddate,7,2)as char(2)) +'/'+
cast(SUBSTRING(AccountOneWays.enddate,5,2)as char(2))+'/'+
cast(SUBSTRING(AccountOneWays.enddate,1,4)as char(4)) as [End Date]
,AccountOneWays.cost as [Cost]
,AccountOneWays.ID as [ID]
FROM AccountOneWays
INNER JOIN Locations outlocation
on outlocation.ID = AccountOneWays.start_location
INNER JOIN Locations inlocation
on inlocation.ID = AccountOneWays.end_location
WHERE AccountOneWays.Account = (SELECT ID FROM Accounts WHERE Account = 'Alamo')
AND AccountOneWays.start_location IN (
SELECT ID
FROM locations
WHERE locationname IN ('Allentown Arpt')
)
ORDER BY [End Date] DESC
,[Start Date] DESC
,[Out Location] ASC
,[In Location] ASC
您的帮助将不胜感激