mysql 查找最高价格与日期

j5fpnvbx  于 2022-12-17  发布在  Mysql
关注(0)|答案(2)|浏览(103)

我是SQL新手。在我的查询中,我需要max closeprice(column-name)和date(column-name)。那一年的每个月。我创建了这个查询:

Select max(close), date from commodity where commodity ='rice' and location= 'pune' year(date) ='2022' group by month(date) order by month(date) desc;

在这个查询中,我得到了一个最高收盘价,但我没有得到实际的最高收盘价日期。我得到了一个最高收盘价的最高日期。示例
我想要这个:该日期的实际最高收盘价和最高收盘价

date         close     
 2022-10-15   5600
 2022-09-11   6200

我得到的是:最大日期和最大关闭图片

date         close     
 2022-10-31   5600
 2022-09-30   6200
qvsjd97n

qvsjd97n1#

试试这个:

select date ,close
from commodity c
join
    (Select max(close) monthly_mx, month(date) mon 
    from commodity 
    where commodity ='rice' and location= 'pune' and year(date) ='2022' 
    group by month(date) ) t
on c.close =monthly_mx and month(c.date)=mon
where c.commodity ='rice' and c.location= 'pune' and year(c.date) ='2022' 
order by month(date) desc;
c3frrgcw

c3frrgcw2#

一个简单的GROUP BY on date列将帮助您获取此类数据

select date ,close ,  MAX(price) as maxPrice
from commodity
where commodity ='rice' and location= 'pune' and year(date) ='2022' 
GROUP BY test_one.date 
order by month(date) desc;

相关问题