我有另一个查询来选择返回最小值,最大值,开始和结束价格。 它适用于特定月份,适用于metal_id = 1,但当更改为2时,它不会返回任何数据。
请看: 下面的查询可以选择给定月份中每天的最小,最大,开始和最后价格。
我想选择相同的但是整个月,就像显示给定月份的整体表现而不是每天。
小提琴: http://sqlfiddle.com/#!9/bee86/1
我只需要最后2个价格,所选月份的第一个和最后一个价格......
select
highp.metal_price_datetime_IST AS high_price_metal_price_datetime_IST
, highp.metal_price as highest_price
, lowp.report_term
, lowp.metal_price as lowest_price
, lowp.metal_price_datetime_IST AS low_price_metal_price_datetime_IST
from (select @report_term:=concat(monthname(metal_price_datetime_IST), ' ', year(metal_price_datetime_IST)) as report_term
, metal_price_datetime_IST
, metal_price
, metal_id
, case when @report_term=@old_report_term then @rn1:=@rn1+1 else @rn1:=1 end as rn
, @old_report_term:=@report_term
from metal_prices
cross join (select @rn1:=0, @old_report_term:='') inituservar1
where datediff(now(), metal_price_datetime_IST) between 0 and 180
and metal_id = 1
order by metal_id, report_term, metal_price asc) lowp
inner join (select @report_term2:=concat(monthname(metal_price_datetime_IST), ' ', year(metal_price_datetime_IST)) as report_term
, metal_price_datetime_IST
, metal_price
, metal_id
, case when @report_term2=@old_report_term2 then @rn2:=@rn2+1 else @rn2:=1 end as rn
, @old_report_term2:=@report_term2
from metal_prices
cross join (select @rn2:=0, @old_report_term2:='') inituservar1
where datediff(now(), metal_price_datetime_IST) between 0 and 180
and metal_id = 1
order by metal_id, report_term, metal_price desc) highp
on lowp.rn=highp.rn
and lowp.metal_id = highp.metal_id
and lowp.report_term = highp.report_term
and lowp.rn = 1
order by lowp.metal_price_datetime_IST DESC