mysql - How to get last 3 data of last 3 week days from a table -
i have table consisting of stock market's daily data. consists of various number of rows each day.i have column named 'high' in table. need calculate max(high) last 3 week days. ex:- if today wednesday need calculate max of last week's friday , week's monday,tuesday. know can if know date manually using query this.
select max(high) table_name date>='date'
but don't want want automate program written in php. how can achieve in both either php or sql appreciable. table has 6 columns
date,time,open,high,low,close
say suppose if table this
date time open high low close 2015-05-06 09:30:00 2012.50 2020.5 2016.5 2014.0 2015-05-06 09:31:00 2013.50 2021.5 2014.5 2016.0 2015-05-06 09:32:00 2014.50 2021.75 2017.5 2013.0 2015-05-07 09:30:00 2011.50 2019.5 2018.5 2014.0 2015-05-07 09:31:00 2014.50 2022.5 2016.5 2015.0 2015-05-07 09:32:00 2012.50 2026.5 2017.5 2016.0 2015-05-08 09:30:00 2010.50 2024.5 2015.5 2017.0 2015-05-08 09:31:00 2013.50 2026.5 2017.5 2018.0 2015-05-08 09:32:00 2014.50 2028.5 2015.5 2019.0 2015-05-08 09:33:00 2014.50 2022.5 2017.5 2012.0 2015-05-11 09:30:00 2017.50 2025.5 2018.5 2013.0 2015-05-11 09:31:00 2018.50 2027.5 2019.5 2016.0 2015-05-11 09:32:00 2019.50 2024.5 2011.5 2017.0 2015-05-11 09:33:00 2020.50 2026.5 2017.5 2014.0 2015-05-12 09:30:00 2018.50 2023.5 2018.5 2018.0 2015-05-12 09:31:00 2017.50 2024.5 2017.5 2014.0 2015-05-12 09:32:00 2018.50 2023.5 2018.5 2013.0 2015-05-12 09:33:00 2017.50 2024.5 2019.5 2014.0 2015-05-12 09:34:00 2016.50 2023.5 2016.5 2012.0 2015-05-12 09:35:00 2017.50 2025.5 2018.5 2011.0
and if today's date 2015-05-13(wednesday) need max(high) of last 3 week days i.e 2015-05-12,11,08 2028.5.
presumably, have data on "week days".
select max(high) table_name t join (select date table_name group date order date desc limit 3 ) dates on t.date = dates.date;
presumably, either want condition on stock or group by
, based on sample query.
you can make more efficient adding where
clause. typically, last 3 working days within last week, or so:
select max(high) table_name t join (select date table_name date >= date_sub(curdate(), interval 7 day) group date order date desc limit 3 ) dates on t.date = dates.date;
Comments
Post a Comment