我应该在下面的查询中更改什么?
SELECT
ERI.ATTACK_CASE_ID,
LRM.REF_VALUE AS ATTACK_CASE_STATUS,
WDR.RESTRICTION_NAME,
LL.USER_LABEL AS ***RESTRICTION_STATUS***,
WSRI.RESTRICTION_DATE,
CASE NVL(ERI.IS_PRIMARY, 0) WHEN 0 THEN 'No' ELSE CASE ERI.IS_PRIMARY WHEN 1 THEN 'Yes' ELSE 'No' END END AS PRIMARY
FROM ALERT A
LEFT OUTER JOIN ENTITY_RESTRICTION_INFO ERI ON A.ALERT_KEY=ERI.ENTITY_KEY
LEFT OUTER JOIN LK_REFERENCE_MAPPING LRM ON LRM.REF_KEY= TO_CHAR(ERI.ATTACK_CASE_STATUS)
LEFT OUTER JOIN LK_REFERENCE_CATEGORY LRG ON LRM.LK_REFERENCE_CATEGORY_ID = LRG.ID
AND LRG.CATEGORY_CODE = 'DD'
AND LRG.SUB_CATEGORY_CODE='ATTACK_STATUS_ID_NAME'
LEFT OUTER JOIN WNORKOM_DD_RESTRICTION WDR ON ERI.RESTRICTION_ID=WDR.RESTRICTION_ID
LEFT OUTER JOIN WNORKOM_SAR_RESTRICT_INFO WSRI ON WSRI.RESTRICTION_ID=ERI.RESTRICTION_ID
*LEFT OUTER JOIN LK_LOOKUPS LL ON LL.CODE=**WSRI.RESTRICTION_STATUS***
WHERE A.ALERT_KEY=121234
AND ERI.ATTACK_CASE_ID='PP-123-0980'
AND LL.TYPE=9502
当 WSRI.RESTRICTION_STATUS 中有某个值时,上面的查询会返回所有记录。
但是,当 WSRI.RESTRICTION_STATUS = NULL 时,它会返回 NO RECORDS。 我可以在上面的查询中添加/更改什么,以便当 WSRI.RESTRICTION_STATUS = NULL 时 LL.USER_LABEL = 'Unknown'。供您参考,LK_LOOKUPS 表中没有值“未知”。当 WSRI.RESTRICTION_STATUS = NULL 时,我希望将其作为独立字符串返回。使用 LL.CODE=NULL 和 LL.USER_LABEL='Unknown' 将记录添加到 LK_LOOKUPS 表中可能会弄乱事情,因此请帮助我在查询中独立传递它。
提前致谢。
SELECT
ERI.ATTACK_CASE_ID,
LRM.REF_VALUE AS ATTACK_CASE_STATUS,
WDR.RESTRICTION_NAME,
LL.USER_LABEL AS ***RESTRICTION_STATUS***,
WSRI.RESTRICTION_DATE,
CASE NVL(ERI.IS_PRIMARY, 0) WHEN 0 THEN 'No' ELSE CASE ERI.IS_PRIMARY WHEN 1 THEN 'Yes' ELSE 'No' END END AS PRIMARY
FROM ALERT A
LEFT OUTER JOIN ENTITY_RESTRICTION_INFO ERI ON A.ALERT_KEY=ERI.ENTITY_KEY
LEFT OUTER JOIN LK_REFERENCE_MAPPING LRM ON LRM.REF_KEY= TO_CHAR(ERI.ATTACK_CASE_STATUS)
LEFT OUTER JOIN LK_REFERENCE_CATEGORY LRG ON LRM.LK_REFERENCE_CATEGORY_ID = LRG.ID
AND LRG.CATEGORY_CODE = 'DD'
AND LRG.SUB_CATEGORY_CODE='ATTACK_STATUS_ID_NAME'
LEFT OUTER JOIN WNORKOM_DD_RESTRICTION WDR ON ERI.RESTRICTION_ID=WDR.RESTRICTION_ID
LEFT OUTER JOIN WNORKOM_SAR_RESTRICT_INFO WSRI ON WSRI.RESTRICTION_ID=ERI.RESTRICTION_ID
*LEFT OUTER JOIN LK_LOOKUPS LL ON LL.CODE=**WSRI.RESTRICTION_STATUS***
WHERE A.ALERT_KEY=121234
AND ERI.ATTACK_CASE_ID='PP-123-0980'
AND LL.TYPE=9502
The above query returns me all the records when there is some value in WSRI.RESTRICTION_STATUS
However, it returns NO RECORDS when WSRI.RESTRICTION_STATUS = NULL.
What can I add/change in above query so that LL.USER_LABEL = 'Unknown' when WSRI.RESTRICTION_STATUS = NULL. For your information, there is no value 'Unknown' in LK_LOOKUPS table. This is something I want to return as an independent string when WSRI.RESTRICTION_STATUS = NULL. Adding a record to LK_LOOKUPS table with LL.CODE=NULL and LL.USER_LABEL='Unknown' can mess up things so please help me passing it independently in the query.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您只需将条件
LL.Type = 9502
移至 Left Join 并在 Select 子句中使用 Coalesce 即可。此外,由于您要从Where子句将条件应用于Entity_Restriction_Info
表中的列,因此您已将该表的左联接转换为内联接,因此,您不妨使用内联接。It sounds you simply need to move the criteria
LL.Type = 9502
into the Left Join and use Coalesce in the Select clause. Also, since you are applying criteria to a column in theEntity_Restriction_Info
table from the Where clause, you have transformed the Left Join to this table into an Inner Join and thus, you might as well use an Inner Join.使用 LL.CODE=NULL 添加记录不会对您有帮助,因为 NULL=NULL 始终为 null(因此为 false)。您需要使用选择表达式:
删除
LEFT OUTER JOIN LK_LOOKUPS LL ON LL.CODE=WSRI.RESTRICTION_STATUS
和AND LL.TYPE=9502
,然后更改LL.USER_LABEL AS RESTRICTION_STATUS,
为:Adding a record with LL.CODE=NULL wouldn't help you, as NULL=NULL is always null (hence false). You'll need to use a select expression:
Remove the
LEFT OUTER JOIN LK_LOOKUPS LL ON LL.CODE=WSRI.RESTRICTION_STATUS
and theAND LL.TYPE=9502
, then change theLL.USER_LABEL AS RESTRICTION_STATUS,
to:尝试(编辑 - 根据评论):
try (EDIT - as per comment):