从SQL子集返回Max

时间:2013-10-30 12:05:15

标签: sql-server sql-server-2008

我有以下查询,但是在accountoneway表中可能有几行除了成本之外还有相同的数据,但是还有一个date_created列。

我需要做的是返回每个子集具有max 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

您的帮助将不胜感激

1 个答案:

答案 0 :(得分: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