这是我对动态计算字段的查询:
SELECT 5 * (`Mean (all)` +0.01735568 )/(0.07268901) AS rank1,
5 * (`CVaR 95` +0.51612 )/(0.53212) AS rank2,
5 * (`Hurst` - 0.2 )/(0.7717653) AS rank3,
5 * (`MaxDD` +6.200000762939453 )/(16.200000762939) AS rank4,
5 * (`Positive % 12` - 0.3 )/(1) AS rank5
FROM `quant1`
它工作正常,但我需要添加另一个动态字段,它等于rank1 + rank2 ... + rank 5.所以,我要添加另一个字段,如
SELECT 5 * (`Mean (all)` +0.01735568 )/(0.07268901) AS rank1,
5 * (`CVaR 95` +0.51612 )/(0.53212) AS rank2,
5 * (`Hurst` - 0.2 )/(0.7717653) AS rank3,
5 * (`MaxDD` +6.200000762939453 )/(16.200000762939) AS rank4,
5 * (`Positive % 12` - 0.3 )/(1) AS rank5,
rank1+rank2+rank3+rank4+rank5 AS rank
FROM `quant1`
它会生成错误“unknown column rank1”。有趣的是,完全相同的查询可以正常使用MS Access。
我该如何解决这个问题?
谢谢!
答案 0 :(得分:2)
您不能在同一SELECT
语句中使用别名,而是将其放在子查询中,如下所示:
SELECT rank1, rank2, rank3, rank4, rank5,
rank1 + rank2 + rank3 + rank4 + rank5 AS rank
FROM
(
SELECT 5 * (`Mean (all)` +0.01735568 )/(0.07268901) AS rank1,
5 * (`CVaR 95` +0.51612 )/(0.53212) AS rank2,
5 * (`Hurst` - 0.2 )/(0.7717653) AS rank3,
5 * (`MaxDD` +6.200000762939453 )/(16.200000762939) AS rank4,
5 * (`Positive % 12` - 0.3 )/(1) AS rank5
FROM `quant1`
) AS t
答案 1 :(得分:1)
你必须有办法。 1.将这些查询用作子查询:
select R.*,R.rank1+R.rank2+R.rank3+R.rank4+R.rank5 AS rank from (
SELECT 5 * (`Mean (all)` +0.01735568 )/(0.07268901) AS rank1,
5 * (`CVaR 95` +0.51612 )/(0.53212) AS rank2,
5 * (`Hurst` - 0.2 )/(0.7717653) AS rank3,
5 * (`MaxDD` +6.200000762939453 )/(16.200000762939) AS rank4,
5 * (`Positive % 12` - 0.3 )/(1) AS rank5
FROM `quant1`
) as R
或
2。
在您的查询中替换:
等级-1 +秩2 + rank3 + rank4 + RANK5
用(5 *(Mean (all)
+0.01735568)/(0.07268901))+(5 *(CVaR 95
+0.51612)/(0.53212)).. .. ..
答案 2 :(得分:1)
在MySQL中,你不能引用其他别名,但你可以在select中定义变量,所以试试这个:
SELECT
@rank1 := 5 * (`Mean (all)` +0.01735568 )/(0.07268901) AS rank1,
@rank2 := 5 * (`CVaR 95` +0.51612 )/(0.53212) AS rank2,
@rank3 := 5 * (`Hurst` - 0.2 )/(0.7717653) AS rank3,
@rank4 := 5 * (`MaxDD` +6.200000762939453 )/(16.200000762939) AS rank4,
@rank5 := 5 * (`Positive % 12` - 0.3 )/(1) AS rank5,
@rank1 + @rank2 + @rank3 + @rank4 + @rank5 AS rank
FROM `quant1`
这很难看,但确实有效。
答案 3 :(得分:0)
尝试子查询,例如:
SELECT *, rank1+rank2+rank3+rank4+rank5 AS rank
FROM (
SELECT 5 * (`Mean (all)` +0.01735568 )/(0.07268901) AS rank1,
5 * (`CVaR 95` +0.51612 )/(0.53212) AS rank2,
5 * (`Hurst` - 0.2 )/(0.7717653) AS rank3,
5 * (`MaxDD` +6.200000762939453 )/(16.200000762939) AS rank4,
5 * (`Positive % 12` - 0.3 )/(1) AS rank5,
FROM `quant1`
) QuantRank