Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming that you want the ids of the records with the lowest low_price and the highest high_price you could add these two columns to your query,</p> <pre><code>SELECT (SELECT id ORDER BY low_price ASC LIMIT 1) low_price_id, (SELECT id ORDER BY high_price DESC LIMIT 1) high_price_id, MIN(low_price), MAX(high_price), open, close FROM symbols WHERE date BETWEEN(.. ..) GROUP BY YEARWEEK(date) </code></pre> <p>If efficiency is an issue you should add a column for 'year_week', add some covering indexes, and split the query in two.</p> <p>The 'year_week' column is just an INT set to the value of YEARWEEK(date) and updated whenever the 'date' column is updated. This way you don't have to recalculate it for each query and you can index it.</p> <p>The new covering indexes should look like this. The ordering is important. KEY yw_lp_id (year_week, low_price, id), KEY yw_hp_id (year_week, high_price, id)</p> <p>You should then use these two queries </p> <pre><code>SELECT (SELECT id ORDER BY low_price ASC LIMIT 1) low_price_id, MIN(low_price), open, close FROM symbols WHERE year_week BETWEEN(.. ..) GROUP BY year_week </code></pre> <p>and</p> <pre><code>SELECT (SELECT id ORDER BY high_price DESC LIMIT 1) high_price_id, MAX(high_price), open, close FROM symbols WHERE year_week BETWEEN(.. ..) GROUP BY year_week </code></pre> <p>Covering indexes are pretty useful. Check <a href="http://peter-zaitsev.livejournal.com/6949.html" rel="nofollow noreferrer">this</a> out for more details.</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.
    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