MYSQL,多列分组

时间:2018-10-21 08:57:08

标签: mysql group-by

我有两个桌子。样本数据查询: cmadjprice:

async function imageDetailsCache(images) {
var imageData = {};
var promises = [];
for(let i = 0; i < images.length; i++) {
    try {
        imageData[images[i]] = {
            'BIG': await imageDetails(images[i], 'BIG') ,
            'SMALL': await imageDetails(images[i], 'SMALL')
        }
    } catch (e)  {
        console.log(e);
    }
}
return imageData;

BB03表输出示例查询

select symbol,close,timestamp from cmadjprice;
ABCD,815.9,2014-10-31
ABCD,808.85,2014-11-03
ABCD,797.4,2014-11-05
ABCD,776.55,2014-11-07
ABCD,800.85,2014-11-10
ABCD,808.9,2014-11-11
ABCD,826.8,2014-11-12
ABCD,856.45,2014-11-13
ABCD,856.65,2014-11-14

寻找同一日期的最高收盘价。

select symbol,enter_dt,enter_price,exit_dt,exit_price from bb03 ;
ABCD,2014-10-31,815.90,2018-07-27,1073.60

我没有得到输出?请帮忙 预期产量

select a.symbol, max(a.close) ,a.timestamp  from  cmadjprice a
inner join BB03 b on a.symbol = b.symbol
where a.timestamp between b.enter_dt and b.exit_dt
group by a.symbol,a.timestamp;

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

select a.symbol, max(cast(a.close as DECIMAL(5,2)))  from  cmadjprice a
inner join BB03 b on a.symbol = b.symbol
where a.timestamp between b.enter_dt and b.exit_dt
group by a.symbol;

答案 1 :(得分:0)

我认为这是您需要的:
请注意,如果在与给定符号相关的所有时间戳序列中,最大收盘价均出现多次,则可能返回多个记录。

select c.symbol, c.maxval, d.timestamp from 
(
select 
a.symbol, 
max(a.close) as maxval 
from  
cmadjprice a
inner join BB03 b 
on a.symbol = b.symbol 
where a.timestamp between b.enter_dt and b.exit_dt
group by a.symbol
) c
inner join
cmadjprice d
on c.symbol = d.symbol and c.maxval = d.close
;