简体   繁体   中英

how to select two different date in mysql?

I'm new to database and MySQL. I'm developing a stock tracking software backend with MySQL database. I have a problem in the MYSQL query.

I need to track price change for a certain period. how can I select two different column and row based on date. for example I need MYSQL to return 'open' value from date '2019-02-27' and 'close' value from date '2019-03-01';
and calculate the % differences in between two value(which is Decimal).

Is it possible to do this kind of query in MYSQL or I should write program which send two query. one to get 'open' from '2019-02-27' and other to get 'close' from '2019-03-01'.

here is the SQL fiddle for my problem http://sqlfiddle.com/#!9/eb23e3/6

here is any example table

symbol |    date    |  open | close | low   | high 
----------------------------------------------------
   HCL | 2019-02-27 | 36.00 | 38.00 | 34.00 | 40.00
   HCL | 2019-02-28 | 37.00 | 39.00 | 36.00 | 41.00
   HCL | 2019-03-01 | 38.00 | 42.00 | 37.00 | 46.00

how can I get 'open' from date '2019-02-27' AND 'close' from date '2019-03-01' and then calculated the % difference like (2019-02-27) 'open' value is 36.00 and (2019-03-01) 'close' value is 42.00 so the % percentage difference is +16.6%.

With this you get the 3 needed columns:

select t.*, 
  concat(
    case when t.close > t.open then '+' else '' end,
    truncate(100.0 * (t.close - t.open) / t.open, 1)
  ) percentdif
from (
  select 
    (select open from dailyprice where date = '2019-02-27') open, 
    (select close from dailyprice where date = '2019-03-01') close
) t

See the demo

我建议运行两个不同的查询,然后计算返回值的差异

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM