I have a table consisting of stock market's daily data. It consists of various number of rows for each day.I have a column named 'High' in the table. Now I need to calculate MAX(High) for last 3 week days. For ex:- if today is Wednesday I need to calculate MAX of last week's friday and this week's monday,tuesday. I know that I can do this if I know the date manually using a query like this.
Select MAX(HIGH) from table_name where date>='date'
But I don't want to do like this i just want to automate this with a program written in PHP. How can i Achieve this any help in both either PHP or SQL is appreciable. My table just has 6 columns
date,time,open,high,low,close
say suppose if my table is like 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 is 2015-05-13(wednesday) I need MAX(high) of last 3 week days ie 2015-05-12,11,08 which is 2028.5.
Presumably, you only have data on "week days".
select max(high)
from table_name t join
(select date
from table_name
group by date
order by date desc
limit 3
) dates
on t.date = dates.date;
Presumably, you either want a condition on the stock or a group by
, but this is based on your sample query.
You can make this more efficient by adding a where
clause. Typically, the last three working days would be within the last week, or so:
select max(high)
from table_name t join
(select date
from table_name
where date >= date_sub(curdate(), interval 7 day)
group by date
order by date desc
limit 3
) dates
on t.date = dates.date;
您可以在这种情况下使用date_sub函数:
select max(high) from table_name where date > date_sub(date, INTERVAL 3 DAY);
我认为这是正确的查询:
select top 3 date, max(high) from table_name order by date desc
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.