分组问题

时间:2013-09-19 08:17:43

标签: mysql sql

我有两个表response_archive 27 000 000行,以及包含4 000行的机场。

我试图选择的字段:

  • response_archive.min(价格)
  • response_archive.avg(价格)
  • response_archive.year_week< - (group by,indexed)
  • response_archive.from(与airport.IATA合作)
  • response_archive.to(与airport.IATA合作)

  • response_archive.from和response_archive.to不能相同。

  • response_archive.year_week应该在一个范围内,如查询中所示。

我遇到了分组问题,我无法通过response_archive.year_week进行分组,同时获得最低/平均价格。

这是我当前的查询,它远非最佳,并且需要大约90秒才能执行。

SELECT
    `from`,
    `to`,
    year_week,
    min(price) min_price,
    round(AVG(price)) avg_price
  FROM response_archive
   INNER JOIN airports AS `f` ON (f.IATA = `from`)
   INNER JOIN airports AS `t` ON (t.IATA = `to`)
 WHERE
    year_week < '1310' AND
    year_week > '1210' AND
    returntrip = 1 AND
    adults = 1 AND
    children = 0 AND
    `price` < 70000 AND
    `price` > 80 AND
    f.SweCity = 'Stockholm' AND
    t.CountryCode = 'US'
GROUP BY year_week
ORDER BY year_week;

目前的结果:

from   to     year_week min   avg
STO    NYC    1211      3552  6311
ARN    LAX    1212      3097  6496
STO    NYC    1213      3532  7379
ARN    NYC    1214      3584  6635
STO    LAX    1215      3523  5907
STO    FLL    1216      3559  5698
STO    NYC    1217      3642  5919

除了min / avg不正确。

比较第一个值是否正确:

SELECT
    min(price) min,
    '3532' min_expected,
    round(avg(price)) avg,
    '7379' avg_expected
FROM
    response_archive
WHERE
    `from` = 'STO' AND
    `to` = 'NYC' AND
    year_week = '1213' AND
    returntrip = 1 AND
    adults = 1 AND
    children = 0 AND
    `price` < 70000 AND
    `price` > 80;

正确的结果:

min     min_exp avg     avg_exp
3532    3532    5955    7379

有人能指出我的方向或解释为什么它没有给我我想要的结果。

2 个答案:

答案 0 :(得分:0)

以下是您的查询简化版,并添加了缺少的GROUP BY字段:

SELECT
    `from`,
    `to`,
    year_week,
    min(price) min_price,
    round(AVG(price)) avg_price
  FROM response_archive
   INNER JOIN airports AS `f` ON (f.IATA = `from`)
   INNER JOIN airports AS `t` ON (t.IATA = `to`)
 WHERE
    year_week < '1310' AND
    year_week > '1210' AND
    returntrip = 1 AND
    adults = 1 AND
    children = 0 AND
    `price` < 70000 AND
    `price` > 80 AND
    f.CountryCode = 'SE' AND
    t.CountryCode = 'US'
GROUP BY `from`, `to`, year_week
ORDER BY year_week;

答案 1 :(得分:0)

你可以避免表x并将条件放在外部的位置。试试这个:

SELECT 
    `from`,
    `to`,
    year_week,
    min(price) min_price,
    round(AVG(price)) avg_price     
FROM
    response_archive AS r
        INNER JOIN airports AS `f` ON (f.IATA = `from`)
        INNER JOIN airports AS `t` ON (t.IATA = `to`),
WHERE
    r.year_week < '1310' AND
    r.year_week > '1210'        
    returntrip = 1 AND
    adults = 1 AND
    children = 0 AND
    `price` < 70000 AND
    `price` > 80 AND
    f.CountryCode = 'SE' AND
    t.CountryCode = 'US'
group by `from`,`to`, year_week