在我的SQL查询中,我需要对别名进行一些算术运算。
SELECT
MY_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME1,
MY_SECOND_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME2,
MYALIASNAME1 - MYALIASNAME2 AS MYALIASHNAME3
FROM
MYTABLE
但是,这不起作用,因为它不会将MYALIASNAME1
和MYALIASNAME2
视为列。任何想法我怎样才能做到这一点?
我正在使用H2,特别是h2-1.3.173.jar。我在服务器模式下使用它。
感谢。
答案 0 :(得分:3)
试试这个。
SELECT X.MYALIASNAME1 - X.MYALIASNAME2 AS MYALIASNAME3 FROM
(
SELECT
MY_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME1,
MY_SECOND_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME2
FROM
MYTABLE
)X
答案 1 :(得分:1)
use cte expressions
用cte作为 (
select MY_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME1,
MY_SECOND_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME2
from table
)
select MYALIASNAME1 - MYALIASNAME2 AS MYALIASHNAME3 from cte
答案 2 :(得分:1)
不要使用别名
SELECT
MY_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME1,
MY_SECOND_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME2,
MY_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN - MY_SECOND_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASHNAME3
FROM
MYTABLE
或使用中间步骤
SELECT MYALIASNAME1, MYALIASNAME2, MYALIASNAME1 - MYALIASNAME2 AS MYALIASHNAME3
FROM (
SELECT
MY_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME1,
MY_SECOND_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME2
FROM
MYTABLE
) TableAlias
答案 3 :(得分:0)
它会起作用......
"SELECT
MY_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME1,
MY_SECOND_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN AS MYALIASNAME2,
(MY_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN) - (MY_SECOND_COMPLEX_EXPRESSION_USING_SUM_AND_CASEWHEN) AS MYALIASHNAME3
FROM
MYTABLE"