在我的SQL查询中,我需要对位于嵌套选择中的别名进行基于别名的算法。
我的查询如下:
SELECT P.EMPLOYEE_NO, EE.BASIC_SALARY,
(SELECT SUM (NEW_ACTIVITY_VALUE)
FROM
(SELECT
(CASE WHEN P.ACTIVITY_TYPE = '0' THEN (P.ACTIVITY_VALUE * (-1))
ELSE P.ACTIVITY_VALUE
END) AS NEW_ACTIVITY_VALUE
FROM V_EMPLOYEE_PAYACT P
WHERE P.ACTIVITY_TYPE IN ('0','1') AND P.EMPLOYEE_NO = '00002789' AND
((YEAR = 2014 AND MONTH <= 4)
OR
(YEAR > 2013 AND YEAR <2014)
OR
(YEAR = 2013 AND MONTH >= 5))
) t
) AS SUM_ACTIVITY_VALUE,
(SELECT NEW_BASIC_SALARY FROM
(SELECT (EE.BASIC_SALARY- SUM_ACTIVITY_VALUE) AS NEW_BASIC_SALARY
FROM EMPLOYEE_EMPLOYMENT EE
) AS X
FROM EMPLOYEE_EMPLOYMENT EE
JOIN V_EMPLOYEE_PAYACT P
ON EE.EMPLOYEE_ID = P.EMPLOYEE_ID
WHERE P.EMPLOYEE_NO = '00002789'
GROUP BY P.EMPLOYEE_NO, EE.BASIC_SALARY
我现在唯一的问题是这个部分:
..(SELECT NEW_BASIC_SALARY FROM
(SELECT (EE.BASIC_SALARY- SUM_ACTIVITY_VALUE) AS NEW_BASIC_SALARY
FROM EMPLOYEE_EMPLOYMENT EE
) AS X...
如果我从查询中扣除上述有问题的部分,我得到的结果如下:
EMP_NO | s BASIC_SALARY | s SUM_ACTIVITY_VALUE | s
1234-S-S-S | 2009年-S-S-S-S-S-S-S | 100-S-S-S-S-S-S-S-S-S |
我需要的结果如下:
EMP_NO | s BASIC_SALARY | s SUM_ACTIVITY_VALUE | s NEW_BASIC_SALARY | s
1234-S-S-S | 2009年-S-S-S-S-S-S-S | 100-S-S-S-S-S-S-S-S-S-S-S | 1909年-S-S-S-S-S-S-S |
答案 0 :(得分:1)
您的查询中可能存在其他缺陷,但是如果您要获取要用功能查询减去的值,那么您可以简单地将整个事物作为子查询并使用别名进行减法。 :
SELECT *,BASIC_SALARY - SUM_ACTIVITY_VALUE as NEW_BASIC_SALARY
FROM (
SELECT P.EMPLOYEE_NO
, EE.BASIC_SALARY
,(SELECT SUM (NEW_ACTIVITY_VALUE)
FROM (SELECT (CASE WHEN P.ACTIVITY_TYPE = '0'
THEN (P.ACTIVITY_VALUE * (-1))
ELSE P.ACTIVITY_VALUE
END) AS NEW_ACTIVITY_VALUE
FROM V_EMPLOYEE_PAYACT P
WHERE P.ACTIVITY_TYPE IN ('0','1')
AND P.EMPLOYEE_NO = '00002789'
AND ((YEAR = 2014 AND MONTH <= 4)
OR (YEAR > 2013 AND YEAR <2014)
OR (YEAR = 2013 AND MONTH >= 5))
) t
) AS SUM_ACTIVITY_VALUE
FROM EMPLOYEE_EMPLOYMENT EE
JOIN V_EMPLOYEE_PAYACT P
ON EE.EMPLOYEE_ID = P.EMPLOYEE_ID
WHERE P.EMPLOYEE_NO = '00002789'
GROUP BY P.EMPLOYEE_NO, EE.BASIC_SALARY
) sub