我正在使用 SQL 2008,只是无法让这个查询工作。我有一张表,里面有航班价格的详细信息。每条路线每天都有很多行,我想返回路线每天的最低价格以及 DateAdded(添加行的日期时间)。我快到了,因为我似乎设法获得了每天最低的退货价格,但我似乎无法获得正确的退货日期。下面的代码似乎可以返回每天的最低价格,但我认为我需要某种加入来返回 DateAdded?
ALTER PROCEDURE [dbo].[FlightInfoLowestPricePerDay]
(
@AirportFrom varchar(5),
@AirportTo varchar(5)
)
AS
select DATEADD(dd, 0, DATEDIFF(dd, 0, TimeDeparture)) as FlightDate, MIN(Price) as MinPrice
from FlightInfo
where AirportFrom = @AirportFrom and AirportTo = @AirportTo
AND TimeDeparture > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
Group By DATEADD(dd, 0, DATEDIFF(dd, 0, TimeDeparture))
Order by FlightDate ASC
我已经尝试过诸如下面的代码之类的东西,但是我还没有完全理解联接,因此已经挣扎了很长时间,尽管我确定我错过了一些非常简单的东西!
ALTER PROCEDURE [dbo].[FlightInfoLowestPricePerDay]
(
@AirportFrom varchar(5),
@AirportTo varchar(5)
)
AS
select DATEADD(dd, 0, DATEDIFF(dd, 0, fi1.TimeDeparture)) as FlightDate, MIN(fi1.Price) as MinPrice, fi2.DateAdded
from FlightInfo
fi1 join FlightInfo fi2 on fi1.Price = fi2.Price
where fi1.AirportFrom = @AirportFrom and fi1.AirportTo = @AirportTo
AND fi1.TimeDeparture > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
Group By DATEADD(dd, 0, DATEDIFF(dd, 0, fi1.TimeDeparture))
Order by FlightDate ASC