3

我有以下查询,但是 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

您的帮助将不胜感激

4

1 回答 1

3

您可以使用排名功能,如ROW_NUMBER

WITH cte 
     AS (SELECT [Out Location] = outlocation.locationname,
                [In Location]  = inlocation.locationname,
                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], 
                Cost = accountoneways.cost,
                ID = accountoneways.id,
                RN = ROW_NUMBER() OVER (PARTITION BY AccountOneWays.ID
                                        ORDER BY  date_created DESC) 
         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'))
) 
SELECT *
FROM   CTE 
WHERE  RN = 1 
ORDER  BY [end date] DESC, 
          [start date] DESC, 
          [out location] ASC, 
          [in location] ASC 

ID即使有多个具有最高 的行,这也会为每个记录返回一条记录date_created。如果要返回所有行,请使用DENSE_RANK而不是ROW_NUMBER.

在这里您可以看到所有功能及其工作原理:http ://technet.microsoft.com/en-us/library/ms189798.aspx

于 2013-10-30T12:14:35.893 回答