Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One problem I see right off the bat is using a timestamp data type for the date, this will complicate your sql query for two reasons - you will have to use a range or convert to an actual date in your where clause, but, more importantly, since you state that you are interested in today's closing price and yesterday's closing price, you will have to keep track of the days when the market is open - so Monday's query is different than tue - fri, and any day the market is closed for a holiday will have to be accounted for as well.</p> <p>I would add a column like mktDay and increment it each day the market is open for business. Another approach might be to include a 'previousClose' column which makes your calculation trivial. I realize this violates normal form, but it saves an expensive self join in your query.</p> <p>If you cannot change the structure, then you will do a self join to get yesterday's close and you can calculate the % change and order by that % change if you wish.</p> <p>Below is Eric's code, cleaned up a bit it executed on my server running mysql 5.0.27</p> <pre><code>select p_today.`ticker`, p_today.`date`, p_yest.price as `open`, p_today.price as `close`, ((p_today.price - p_yest.price)/p_yest.price) as `change` from prices p_today inner join prices p_yest on p_today.ticker = p_yest.ticker and date(p_today.`date`) = date(p_yest.`date`) + INTERVAL 1 DAY and p_today.price &gt; 0 and p_yest.price &gt; 0 and date(p_today.`date`) = CURRENT_DATE order by `change` desc limit 10 </code></pre> <p>Note the back-ticks as some of your column names and Eric's aliases were reserved words.</p> <p>Also note that using a where clause for the first table would be a less expensive query - the where get's executed first and only has to attempt to self join on the rows that are greater than zero and have today's date</p> <pre><code>select p_today.`ticker`, p_today.`date`, p_yest.price as `open`, p_today.price as `close`, ((p_today.price - p_yest.price)/p_yest.price) as `change` from prices p_today inner join prices p_yest on p_today.ticker = p_yest.ticker and date(p_today.`date`) = date(p_yest.`date`) + INTERVAL 1 DAY and p_yest.price &gt; 0 where p_today.price &gt; 0 and date(p_today.`date`) = CURRENT_DATE order by `change` desc limit 10 </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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