[英]MySQL CASE from another CASE result in SQL query
I have a fairly large query one part of which is: 我有一个很大的查询,其中一部分是:
...
ROUND(AVG(
CASE RA12.answer
WHEN 'L' THEN 3
WHEN 'P' THEN 2
WHEN 'E' THEN 1
ELSE 0
END
),0) as 'avg_presentation_format_raw'
...
...and it all works fine including the column above. ...而且一切正常,包括上面的列。 What I now need to do is another CASE (or something) on the results of the column above.
我现在需要做的是在上一列的结果中使用另一个CASE(或其他方式)。 However since it's calculated at run time I cannot reference it.
但是,由于它是在运行时计算的,因此我无法引用它。 I am guessing I need to use temp variables or something and I found a few things that come close, but nothing that quite does what I am looking for.
我猜想我需要使用临时变量或某些东西,但是我发现一些接近的东西,但是没有什么能真正满足我的需求。
What I need is something like what is below based on and in addition to the calculated column above: 除了上面的计算列之外,我还需要以下内容:
...
CASE avg_presentation_format_raw <--THIS WON'T WORK BECAUSE IT'S THE CALCULATED COLUMN
WHEN 3 THEN 'Lecture'
WHEN 2 THEN 'Poster'
WHEN 1 THEN 'Either'
ELSE 'Not Specified'
END as 'presentation_format'
...
Any thoughts or ideas appreciated! 任何想法或想法表示赞赏!
TIA TIA
Use subquery like: 使用子查询,如:
SELECT
CASE t.avg_presentation_format_raw
WHEN 3 THEN 'Lecture'
WHEN 2 THEN 'Poster'
WHEN 1 THEN 'Either'
ELSE 'Not Specified'
END AS 'presentation_format'
(...)
FROM
(
SELECT ROUND(AVG(
CASE RA12.answer
WHEN 'L' THEN 3
WHEN 'P' THEN 2
WHEN 'E' THEN 1
ELSE 0
END),0) AS 'avg_presentation_format_raw'
(...)
FROM (...)
) AS t
Or just wrap it: 或者只是包装一下:
SELECT
CASE ROUND(AVG(
CASE answer
WHEN 'L' THEN 3
WHEN 'P' THEN 2
WHEN 'E' THEN 1
ELSE 0
END
),0)
WHEN 3 THEN 'Lecture'
WHEN 2 THEN 'Poster'
WHEN 1 THEN 'Either'
ELSE 'Not Specified'
END as 'presentation_format',
ROUND(AVG(
CASE RA12.answer
WHEN 'L' THEN 3
WHEN 'P' THEN 2
WHEN 'E' THEN 1
ELSE 0
END),0) AS 'avg_presentation_format_raw'
FROM tab
But you cannot reference like this: 但是您不能像这样引用:
SELECT
1 AS 'var1'
var1 + 1 AS 'var2'
All the results of a single row from a select are atomic. 选择中一行的所有结果都是原子的。 That is, you can view them all as if they occur in parallel and cannot depend on each other.
也就是说,您可以将它们全部视为并行发生并且不能相互依赖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.