SQL GROUP BY或JOIN

时间:2013-03-22 10:39:12

标签: sql join group-by

我正在使用SQL 2008,但无法使此查询生效。我有一张包含航班价格详情的表格。每条路线每天都有很多行,我想要返回路线每天的最低价格以及DateAdded(添加行的日期时间)。我几乎在那里,因为我似乎设法得到每天返回的最低价格,但我似乎无法得到正确的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 个答案:

答案 0 :(得分:0)

在分组依据中添加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

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

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

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