Note that there are some explanatory texts on larger screens.

plurals
  1. POIssues with grouping
    primarykey
    data
    text
    <p>I have two tables response_archive 27 000 000 rows, and airports which consist of 4 000 rows.</p> <p>Fields that im trying to select:</p> <ul> <li>response_archive.min(price)</li> <li>response_archive.avg(price)</li> <li>response_archive.year_week &lt;- (group by, indexed)</li> <li>response_archive.from (joined with airport.IATA)</li> <li><p>response_archive.to (joined with airport.IATA)</p></li> <li><p>response_archive.from and response_archive.to cannot be the same.</p></li> <li>response_archive.year_week should be within a span, as shown in the query.</li> </ul> <p>I'm having issues with the grouping, i cannot group by response_archive.year_week and at the same time get the minimum/average price.</p> <p>This is my current query, it's far from optimal, and takes about 90 seconds to perform.</p> <pre><code>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 &lt; '1310' AND year_week &gt; '1210' AND returntrip = 1 AND adults = 1 AND children = 0 AND `price` &lt; 70000 AND `price` &gt; 80 AND f.SweCity = 'Stockholm' AND t.CountryCode = 'US' GROUP BY year_week ORDER BY year_week; </code></pre> <p>Current results:</p> <pre><code>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 </code></pre> <p>Except that the min / avg are incorrect.</p> <p>Comparing if the first value is correct:</p> <pre><code>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` &lt; 70000 AND `price` &gt; 80; </code></pre> <p>Correct result:</p> <pre><code>min min_exp avg avg_exp 3532 3532 5955 7379 </code></pre> <p>Can someone point me in a direction or explain why it's not giving me the results i want.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload