4

我正在使用 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
4

4 回答 4

0

您可以进行两个子查询(一个获取每天的小价格,第二个获取 DateAdded)

SELECT  f1.FlightDate, f1.MinPrice, f2.DateAdded From  
(  
    SELECT  
        DATEADD(dd, 0, DATEDIFF(dd, 0, fi1.TimeDeparture)) as FlightDate,
        MIN(fi1.Price) as MinPrice
    FROM FlightInfo fi1
    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))
) f1
INNER JOIN 
(
    SELECT DISTINCT
        DATEADD(dd, 0, DATEDIFF(dd, 0, fi2.DateAdded)) AS DateAdded, 
        Price FROM FlightInfo fi2
    WHERE fi2.AirportFrom = @AirportFrom and fi2.AirportTo = @AirportTo
        AND fi2.TimeDeparture > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
) f2 ON f2.Price = f1.MinPrice
于 2013-04-19T16:30:16.073 回答
0

在 Group By 中添加 fi2.DateAdded

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, Cast(Convert(varchar(20),fi2.DateAdded,101) as datetime) 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)),Cast(Convert(varchar(20),fi2.DateAdded,101) as datetime)
Order by FlightDate ASC
于 2013-03-22T10:44:01.917 回答
0

好的,我决定简化一下,因为我已经浪费了太多时间,所以现在我将特定日期传递给查询并多次运行查询以获得我需要的结果:

ALTER PROCEDURE [dbo].[FlightInfoLowestPriceDateAddedForSpecificDate]
(
@AirportFrom    varchar(5),
@AirportTo      varchar(5),
@Date           datetime
)

AS

select  top 1 *
from FlightInfo 

where AirportFrom = @AirportFrom and AirportTo = @AirportTo
AND DATEADD(dd, 0, DATEDIFF(dd, 0, TimeDeparture)) = DATEADD(dd, 0, DATEDIFF(dd, 0, @Date))

Order by Price asc
于 2013-03-22T11:53:04.717 回答
0

您想为此使用窗口功能。一种方法是row_number()

select flightDate, price
from (select DATEADD(dd, 0, DATEDIFF(dd, 0, TimeDeparture)) as FlightDate, fi.*,
             ROW_NUMBER() over (partition by airportFrom, airportTo, DATEADD(dd, 0, DATEDIFF(dd, 0, TimeDeparture)) order by price) as seqnum
      from FlightInfo fi
      where AirportFrom = @AirportFrom and AirportTo = @AirportTo and
            TimeDeparture > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
     ) fi
where seqnum = 1

如果您使用的是较新版本的 SQL Server,则可以利用date数据类型:

select flightDate, price
from (select cast(TimeDeparture as date) as FlightDate, fi.*,
             ROW_NUMBER() over (partition by airportFrom, airportTo, cast(TimeDeparture as date) order by price) as seqnum
      from FlightInfo fi
      where AirportFrom = @AirportFrom and AirportTo = @AirportTo and
            TimeDeparture > cast(getdate() as date)
     ) fi
where seqnum = 1
于 2013-03-22T14:31:49.097 回答