Tag et kig på denne reference:SQLFIDDLE
Så din nuværende tabel med OHLC bruges til at udlede px_change, px_pct :)Resultater er blevet rounded
for at vise 4 decimaler i ændring samt procent.Null bruges til at vise ingen prisændring/pct, da null er en bedre repræsentation af ingen prisændring end at have en prisændring på nul;)
Forespørgsel:
select ticker, date_format(date,'%m-%d-%Y') as date, open, high,low,close,
pxchange,concat(round(pxpct*100,4),'%') pxpct
from (select case when ticker <> @pxticker
then @pxclose := null end, p.*, ([email protected]) as pxchange,
([email protected])/@pxclose as pxpct, (@pxclose := close),
(@pxticker := ticker) from pricing p
cross join
(select @pxclose := null, @pxticker := ticker
from pricing
order by ticker, date limit 1) as a
order by ticker, date ) as b
order by ticker, date asc
Resultater:
TICKER DATE OPEN HIGH LOW CLOSE PXCHANGE PXPCT
AAAE 11-26-2012 0.01 0.01 0.01 0.01 (null) (null)
AAAE 11-27-2012 0.013 0.014 0.0083 0.014 0.004 40.0000%
AAAE 11-28-2012 0.014 0.0175 0.014 0.0165 0.0025 17.8571%
AAAE 11-29-2012 0.0175 0.0175 0.0137 0.0137 -0.0028 -16.9697%
AAMRQ 11-26-2012 0.4411 0.53 0.4411 0.529 (null) (null)
AAMRQ 11-27-2012 0.51 0.511 0.461 0.495 -0.034 -6.4272%
AAMRQ 11-28-2012 0.482 0.49 0.43 0.464 -0.031 -6.2626%
AAMRQ 11-29-2012 0.4505 0.459 0.4411 0.459 -0.005 -1.0776%
AAMRQ 11-30-2012 0.45 0.457 0.4455 0.4568 -0.0022 -0.4793%
** OPDATERET MED ET BILLEDE FOR AT VISE PARENTES I HENHOLD TIL OPS ANMODNING I KOMMENTARER ** :)