我有3张桌子要加入:
ALERT_CASE_HEADER
- alert_key
ALERT_ITEM
- entity_key
-status_id
-entity_name='Active Alert'
STATUSES
-name
-id
两个警报表都使用alert_key和entity_key进行链接。仅当两个键具有相同值并且每个可用状态名称为entity_name='Active Alert'
时才会进行大小写的总和。表STATUSES中的id列链接到ALERT_ITEM.status_id
我可以这样做吗?我试着运行脚本但看起来很慢。
SELECT
SUM(
CASE
WHEN s.name = 'Pending'
AND ach.alert_key = ai.entity_key
AND ai.entity_name = 'Active Alert'
THEN 1
ELSE 0
END
) AS PENDING, SUM(
CASE
WHEN s.name = 'new'
AND ach.alert_key = ai.entity_key
AND ai.entity_name = 'Active Alert'
THEN 1
ELSE 0
END
) AS NEW, SUM(
CASE
WHEN s.name = 'cancel'
AND ach.alert_key = ai.entity_key
AND ai.entity_name = 'Active Alert'
THEN 1
ELSE 0
END
) AS CANCEL
FROM
STATUSES s, ALERT_CASE_HEADER ach, ALERT_ITEM ai;
(根据提问者11月14日和17日在9:53的回答本来应该是编辑:)
我正在使用的脚本如下所示,只关注2个连接语句。第二个连接正在工作但不是第一个。
SELECT CUS.ORGUNIT_CODE AS ORGANIZATION_UNITS,
SUM(
CASE
WHEN S.name = 'Pending' THEN 1 ELSE 0
END
) AS PENDING_AUTOCLOSURE,
SUM(
CASE
WHEN S.name = 'New' THEN 1 ELSE 0
END
) AS NEW,
SUM(
CASE
WHEN S.name = 'Under Investigation' THEN 1 ELSE 0
END
) AS UNDER_INVESTIGATION,
SUM(
CASE
WHEN S.Name = 'Escalated' THEN 1 ELSE 0
END
) AS ESCALATED,
SUM(
CASE
WHEN S.Name = 'Recommend True Positive' THEN 1 ELSE 0
END
) AS RECOMMEND_TRUE_POSITIVE,
SUM(
CASE
WHEN S.Name = 'Reopen Under Investigation' THEN 1 ELSE 0
END
) AS REOPEN_UNDER_INVESTIGATION
FROM
STATUSES S,CUSTOMERS CUS,ALERT_ITEM AI
JOIN ALERT_ITEM AI ON S.ID = AI.STATUS_ID
JOIN ALERT_CASE_HEADER ACH ON AI.ENTITY_KEY = ACH.ALERT_KEY
WHERE AI.ENTITY_NAME = 'Active Alert'
AND AI.ORGUNIT_ID = CUS.ORGUNIT_ID
GROUP BY CUS.ORGUNIT_CODE;
答案 0 :(得分:1)
ORA-00904:字符串:无效标识符
原因:输入了列名 要么丢失要么无效。
操作:输入有效的列名称。一个 有效列名必须以字母开头,小于或等于 30个字符,仅由字母数字字符组成 特殊字符$,_和#。如果它包含其他字符,那么 它必须用双引号括起来。它可能不是 保留字。
这些问题存在问题。
FROM
STATUSES S,CUSTOMERS CUS,ALERT_ITEM AI
JOIN ALERT_ITEM AI ON S.ID = AI.STATUS_ID
JOIN ALERT_CASE_HEADER ACH ON AI.ENTITY_KEY = ACH.ALERT_KEY
WHERE AI.ENTITY_NAME = 'Active Alert'
S
S
再次用于列S.ID
列无效 表示STAUTUSES表没有名为ID的列。 ALERT_ITEM AI
FROM
STATUSES S
,CUSTOMERS CUS /* change this to explicit join */
JOIN ALERT_ITEM AI ON S.ID /* <<< wrong column name */ = AI.STATUS_ID
停止在表之间使用逗号。如果你还记得这个,那么你的SQL生活会更容易。
答案 1 :(得分:0)
您可以使用:
SELECT
SUM(
CASE
WHEN s.name = 'Pending' THEN 1 ELSE 0
END
) AS PENDING,
SUM(
CASE
WHEN s.name = 'new' THEN 1 ELSE 0
END
) AS NEW,
SUM(
CASE
WHEN s.name = 'cancel' THEN 1 ELSE 0
END
) AS CANCEL
FROM
STATUSES s
join ALERT_ITEM ai on s.id=ai.status_id
join ALERT_CASE_HEADER ach on ai.entity_key=ach.alert_key
where ai.entity_name = 'Active Alert'
答案 2 :(得分:0)
查看您的代码我建议您以这种方式重构查询
使用表之间的连接而不是条件
SELECT
SUM(
CASE
WHEN s.name = 'Pending'
THEN 1
ELSE 0
END
) AS PENDING
, SUM(
CASE
WHEN s.name = 'new'
THEN 1
ELSE 0
END
) AS NEW
, SUM(
CASE
WHEN s.name = 'cancel'
THEN 1
ELSE 0
END
) AS CANCEL
FROM STATUSES s
INNER JOIN ALERT_ITEM ai ON s.id = ai.status_id
AND ai.entity_name = 'Active Alert'
INNER JOIN ALERT_CASE_HEADER ach ON ach.alert_key = ai.entity_key
新OP代码后更新
不要混合隐式和显式联接
SELECT CUS.ORGUNIT_CODE AS ORGANIZATION_UNITS,
SUM(
CASE
WHEN S.name = 'Pending' THEN 1 ELSE 0
END
) AS PENDING_AUTOCLOSURE,
SUM(
CASE
WHEN S.name = 'New' THEN 1 ELSE 0
END
) AS NEW,
SUM(
CASE
WHEN S.name = 'Under Investigation' THEN 1 ELSE 0
END
) AS UNDER_INVESTIGATION,
SUM(
CASE
WHEN S.Name = 'Escalated' THEN 1 ELSE 0
END
) AS ESCALATED,
SUM(
CASE
WHEN S.Name = 'Recommend True Positive' THEN 1 ELSE 0
END
) AS RECOMMEND_TRUE_POSITIVE,
SUM(
CASE
WHEN S.Name = 'Reopen Under Investigation' THEN 1 ELSE 0
END
) AS REOPEN_UNDER_INVESTIGATION
FROM
STATUSES S
JOIN ALERT_ITEM AI ON S.ID = AI.STATUS_ID
JOIN ALERT_CASE_HEADER ACH ON AI.ENTITY_KEY = ACH.ALERT_KEY
INNER JOIN CUSTOMERS CUS ON AI.ORGUNIT_ID = CUS.ORGUNIT_ID
WHERE AI.ENTITY_NAME = 'Active Alert'
GROUP BY CUS.ORGUNIT_CODE;