无效标识符:sql 中多列的总和
invalid identifier : sum of multiple column in sql
我正在尝试计算此查询中的多个列
SELECT
SUM (CASE WHEN B.ID = 1 THEN 1 END) AS OPD,
SUM (CASE WHEN B.ID = 2 THEN 1 END) AS IPD,
SUM (CASE WHEN B.ID = 3 THEN 1 END) AS DC,
SUM (CASE WHEN B.ID = 4 THEN 1 END) AS PROC,
SUM (CASE WHEN B.ID = 5 THEN 1 END) AS SUR,
(OPD + IPD + PROC) as Total
FROM REF_TB_APP_TRANSACTIONS A,
REF_VW_VISIT_TYPE B
WHERE A.REQ_VISIT_TYPE = B.ID
AND A.TO_EST_CODE = 20068;
但我得到了这个错误 PROC 无效标识符
您不能直接在 SELECT
的 Total
列中添加三个 SUMS
,因为您使用的是这些列的别名。你可以用另一个 SUM CASE
.
做你的 Total
专栏
SELECT
SUM (CASE WHEN B.ID = 1 THEN 1 END) AS OPD,
SUM (CASE WHEN B.ID = 2 THEN 1 END) AS IPD,
SUM (CASE WHEN B.ID = 3 THEN 1 END) AS DC,
SUM (CASE WHEN B.ID = 4 THEN 1 END) AS [PROC],
SUM (CASE WHEN B.ID = 5 THEN 1 END) AS SUR,
SUM (CASE WHEN B.ID IN (1,2,4)THEN 1 END) AS Total
FROM REF_TB_APP_TRANSACTIONS A,
REF_VW_VISIT_TYPE B
WHERE A.REQ_VISIT_TYPE = B.ID
AND A.TO_EST_CODE = 20068;
取决于您使用的 DBMS。您不能对这样别名的列求和,您将不得不使用 sub select 并从那里进行求和。如果您验证您的 DBMS,我们可以创建查询。
如果 MS SQL 下面将起作用。几件事:
PROC 是保留字,所以要么更改它,要么用括号括起来(我选择了括号)。如果您使用 JOINS 与查询方式相比,它也是首选。
SELECT OPD, IPD, DC, [PROC], SUR, (OPD + IPD + [PROC]) as Total
FROM (
SELECT
SUM (CASE WHEN B.ID = 1 THEN 1 END) AS OPD,
SUM (CASE WHEN B.ID = 2 THEN 1 END) AS IPD,
SUM (CASE WHEN B.ID = 3 THEN 1 END) AS DC,
SUM (CASE WHEN B.ID = 4 THEN 1 END) AS [PROC],
SUM (CASE WHEN B.ID = 5 THEN 1 END) AS SUR
FROM REF_TB_APP_TRANSACTIONS A
INNER JOIN REF_VW_VISIT_TYPE B ON A.REQ_VISIT_TYPE = B.ID
WHERE A.TO_EST_CODE = 20068
) SUB
您不能将别名列作为 select
的一部分进行引用,因为在查询执行顺序中,它们尚不存在。
您只需包装您的查询,使其成为 derived table
,然后您可以在外部 select
中引用它们,请参阅:
select OPD, IPD, DC, [PROC], SUR, OPD + IPD + [PROC] as Total from (
SELECT
SUM (CASE WHEN B.ID = 1 THEN 1 END) AS OPD,
SUM (CASE WHEN B.ID = 2 THEN 1 END) AS IPD,
SUM (CASE WHEN B.ID = 3 THEN 1 END) AS DC,
SUM (CASE WHEN B.ID = 4 THEN 1 END) AS [PROC],
SUM (CASE WHEN B.ID = 5 THEN 1 END) AS SUR
FROM REF_TB_APP_TRANSACTIONS A
join REF_VW_VISIT_TYPE B on B.ID=A.REQ_VISIT_TYPE
where A.TO_EST_CODE = 20068
)x
猜测是因为您有一个分号,这是 SQLServer,在这种情况下,您需要在保留字 PROC
周围使用 []
我也正确地 joined
你的桌子,因为它不再是 1989 年了:-0
我正在尝试计算此查询中的多个列
SELECT
SUM (CASE WHEN B.ID = 1 THEN 1 END) AS OPD,
SUM (CASE WHEN B.ID = 2 THEN 1 END) AS IPD,
SUM (CASE WHEN B.ID = 3 THEN 1 END) AS DC,
SUM (CASE WHEN B.ID = 4 THEN 1 END) AS PROC,
SUM (CASE WHEN B.ID = 5 THEN 1 END) AS SUR,
(OPD + IPD + PROC) as Total
FROM REF_TB_APP_TRANSACTIONS A,
REF_VW_VISIT_TYPE B
WHERE A.REQ_VISIT_TYPE = B.ID
AND A.TO_EST_CODE = 20068;
但我得到了这个错误 PROC 无效标识符
您不能直接在 SELECT
的 Total
列中添加三个 SUMS
,因为您使用的是这些列的别名。你可以用另一个 SUM CASE
.
Total
专栏
SELECT
SUM (CASE WHEN B.ID = 1 THEN 1 END) AS OPD,
SUM (CASE WHEN B.ID = 2 THEN 1 END) AS IPD,
SUM (CASE WHEN B.ID = 3 THEN 1 END) AS DC,
SUM (CASE WHEN B.ID = 4 THEN 1 END) AS [PROC],
SUM (CASE WHEN B.ID = 5 THEN 1 END) AS SUR,
SUM (CASE WHEN B.ID IN (1,2,4)THEN 1 END) AS Total
FROM REF_TB_APP_TRANSACTIONS A,
REF_VW_VISIT_TYPE B
WHERE A.REQ_VISIT_TYPE = B.ID
AND A.TO_EST_CODE = 20068;
取决于您使用的 DBMS。您不能对这样别名的列求和,您将不得不使用 sub select 并从那里进行求和。如果您验证您的 DBMS,我们可以创建查询。
如果 MS SQL 下面将起作用。几件事:
PROC 是保留字,所以要么更改它,要么用括号括起来(我选择了括号)。如果您使用 JOINS 与查询方式相比,它也是首选。
SELECT OPD, IPD, DC, [PROC], SUR, (OPD + IPD + [PROC]) as Total
FROM (
SELECT
SUM (CASE WHEN B.ID = 1 THEN 1 END) AS OPD,
SUM (CASE WHEN B.ID = 2 THEN 1 END) AS IPD,
SUM (CASE WHEN B.ID = 3 THEN 1 END) AS DC,
SUM (CASE WHEN B.ID = 4 THEN 1 END) AS [PROC],
SUM (CASE WHEN B.ID = 5 THEN 1 END) AS SUR
FROM REF_TB_APP_TRANSACTIONS A
INNER JOIN REF_VW_VISIT_TYPE B ON A.REQ_VISIT_TYPE = B.ID
WHERE A.TO_EST_CODE = 20068
) SUB
您不能将别名列作为 select
的一部分进行引用,因为在查询执行顺序中,它们尚不存在。
您只需包装您的查询,使其成为 derived table
,然后您可以在外部 select
中引用它们,请参阅:
select OPD, IPD, DC, [PROC], SUR, OPD + IPD + [PROC] as Total from (
SELECT
SUM (CASE WHEN B.ID = 1 THEN 1 END) AS OPD,
SUM (CASE WHEN B.ID = 2 THEN 1 END) AS IPD,
SUM (CASE WHEN B.ID = 3 THEN 1 END) AS DC,
SUM (CASE WHEN B.ID = 4 THEN 1 END) AS [PROC],
SUM (CASE WHEN B.ID = 5 THEN 1 END) AS SUR
FROM REF_TB_APP_TRANSACTIONS A
join REF_VW_VISIT_TYPE B on B.ID=A.REQ_VISIT_TYPE
where A.TO_EST_CODE = 20068
)x
猜测是因为您有一个分号,这是 SQLServer,在这种情况下,您需要在保留字 PROC
[]
我也正确地 joined
你的桌子,因为它不再是 1989 年了:-0