我有以下查询:
select ema.es_symbol as symbol, ema.score as score, ema.weight as weight, rsi.relative_strength_index as relative_strength_index
from ema_score ema, relative_strength_index rsi inner join
(select rsi_symbol, max(rsi_date) as maxDate from relative_strength_index group by rsi_symbol) rsiDate
on rsi.rsi_symbol = rsiDate.rsi_symbol
and rsi.rsi_date = rsiDate.maxDate
where ema.es_symbol = rsi.rsi_symbol
and ema.score not in (0,1,10,11)
and rsi.relative_strength_index not in (0,100);
我正在尝试添加一个类似下面的计算列作为最后一列:
ema.weight/max(ema.weight)
我想要的结果是每个符号权重除以权重列中的最大权重。当我按照自己的方式尝试时,我只收到一排结果。我到底错在了什么?
答案 0 :(得分:0)
您必须使用带有MAX
的子查询作为除数,例如:
select ema.weight*1.0/(select max(weight) from #t ema2)
from #t ema
答案 1 :(得分:0)
没有group by子句的任何聚合函数(如max())都会将结果集折叠为单个记录。您需要在子查询中选择最大值,并使用交叉连接将其与所有记录关联。此外,不要混合隐式和显式联接,如果向查询添加外部联接,则可能会产生非常令人讨厌的意外!
select ema.es_symbol as symbol, ema.score as score, ema.weight as weight, ema.weight/maxweight as percentage, rsi.relative_strength_index as relative_strength_index
from ema_score ema
join (select max(weight) as maxweight from ema_score) t
inner join relative_strength_index rsi on ema.es_symbol = rsi.rsi_symbol
inner join
(select rsi_symbol, max(rsi_date) as maxDate from relative_strength_index group by rsi_symbol) rsiDate
on rsi.rsi_symbol = rsiDate.rsi_symbol
and rsi.rsi_date = rsiDate.maxDate
where ema.score not in (0,1,10,11)
and rsi.relative_strength_index not in (0,100);