sorting - MySQL query doesn't sort calculated values -


i have longer, more complex code , want newest entry table, receive oldest one. tried: min(date), max(date) desc, , asc. doesn't work. code require special solution?

select stock_id,        stocks.name,        date_format(date, '%y-%m-%d %h:%i:%s') date,        price, pxchange,        concat(round(pxpct*100, 4), '%') pxpct,         stocks.stockmarket,         stockmarkets.id   (select case when stock_id <> @pxticker @pxclose := null end,                p.*,                 (price - @pxclose) pxchange,                 (price - @pxclose) / @pxclose pxpct,                 (@pxclose := price),                 (@pxticker := stock_id)           quotations p cross join                 (select @pxclose := null, @pxticker := stock_id                   quotations                 order  stock_id, date                 limit  1)         order  stock_id, date) b left   join stocks       on stock_id = stocks.id left   join stockmarkets on stockmarkets.id = stocks.stockmarket   stocks.stockmarket = (select id stockmarkets short ='".$q."') group  stock_id order  stock_id, min(date) asc 

i thinking , working since hours, tried , tried. nothing seems help.

thanks can help. would'nt surprise if slow on uptake.

-- edit - example

what got don't want.

4   facebook, inc.          2014-02-24 14:43:49     7   null    null    1   1 5   tesla motors, inc.  2014-02-24 14:59:28     7   null    null    1   1 

what want (sample values, date - timestamp - important).

4   facebook, inc.          2014-03-10 22:33:39     50  null    null    1   1 5   tesla motors, inc.  2014-03-10 22:52:28     20  null    null    1   1 

with syntax above want latest quotations of every stock connected nasdaq (in example). oldest, first quotations have inserted in table 'quotations'.

edit quotations_table

edit 2 - code

select stock_id,        stocks.name,        date_format(date, '%y-%m-%d %h:%i:%s') date,        price, pxchange,        concat(round(pxpct*100, 4), '%') pxpct,         stocks.stockmarket,         stockmarkets.id   (select case when stock_id <> @pxticker @pxclose := null end,                p.*,                 (price - @pxclose) pxchange,                 (price - @pxclose) / @pxclose pxpct,                 (@pxclose := price),                 (@pxticker := stock_id)           quotations p cross join                 (select @pxclose := null, @pxticker := stock_id                   quotations                 order  date, stock_id                 limit  1)         order  date, stock_id) b left   join stocks       on stock_id = stocks.id left   join stockmarkets on stockmarkets.id = stocks.stockmarket   stocks.stockmarket = (select id stockmarkets short ='nasdaq') group  stock_id order  date, stock_id 

here fiddle: http://www.sqlfiddle.com/#!2/ff809/44 not same data, code working with. , still same problem: how latest entry?

edit - screenshots information structure enter image description here enter image description here enter image description here enter image description here here screenshots content , structure of used tables. maybe helps.

if want oldest record order clause should have date first. change following

order stock_id, date desc 

with

order date desc, stock_id 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -