我应该在下面的查询中更改什么?

时间:2011-10-13 01:44:34

标签: sql oracle oracle10g

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 ='未知'。为了您的信息,LK_LOOKUPS表中没有值“未知”。当 WSRI.RESTRICTION_STATUS = NULL 时,我希望将其作为独立字符串返回。使用LL.CODE = NULL和LL.USER_LABEL ='Unknown'将记录添加到LK_LOOKUPS表可能会搞乱事情,所以请帮我在查询中单独传递它。

提前致谢。

3 个答案:

答案 0 :(得分:0)

尝试(编辑 - 根据评论):

SELECT 
      ERI.ATTACK_CASE_ID, 
      LRM.REF_VALUE AS ATTACK_CASE_STATUS,
      WDR.RESTRICTION_NAME, 
    DECODE ( WSRI.RESTRICTION_STATUS, NULL, 'Unknown', (SELECT USER_LABEL FROM LK_LOOKUPS LL WHERE LL.TYPE=9502 AND LL.CODE=WSRI.RESTRICTION_STATUS)) 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
WHERE A.ALERT_KEY=121234 
AND ERI.ATTACK_CASE_ID='PP-123-0980'

答案 1 :(得分:0)

使用LL.CODE = NULL添加记录对您没有帮助,因为NULL = NULL始终为null(因此为false)。您需要使用选择表达式:

删除LEFT OUTER JOIN LK_LOOKUPS LL ON LL.CODE=WSRI.RESTRICTION_STATUSAND LL.TYPE=9502,然后将LL.USER_LABEL AS RESTRICTION_STATUS,更改为:

IF(WSRI.RESTRICTION_STATUS IS NULL,
  "Unknown", 
  (SELECT USER_LABEL FROM LK_LOOKUPS LL
    WHERE LL.TYPE=9502 AND LL.CODE=WSRI.RESTRICTION_STATUS)
) AS USER_LABEL

答案 2 :(得分:0)

听起来您只需要将条件LL.Type = 9502移到左连接中,并在Select子句中使用Coalesce。此外,由于您要将条件应用于Where子句的Entity_Restriction_Info表中的列,因此您已将左连接转换为此表为内连接,因此,您也可以使用内连接。

Select Eri.Attack_Case_Id
    , Lrm.Ref_Value As Attack_Case_Status
    , Wdr.Restriction_Name
    , Coalesce(LL.User_Label, 'Unknown') 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 As A
    Inner Join Entity_Restriction_Info As Eri 
        On A.Alert_Key=Eri.Entity_Key
    Left Outer Join Lk_Reference_Mapping As Lrm 
        On Lrm.Ref_Key= To_Char(Eri.Attack_Case_Status)
    Left Outer Join Lk_Reference_Category As 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 As Wdr 
        On Eri.Restriction_Id=Wdr.Restriction_Id 
    Left Outer Join Wnorkom_Sar_Restrict_Info As Wsri 
        On Wsri.Restriction_Id=Eri.Restriction_Id
    Left Outer Join Lk_Lookups As LL
        On LL.Code='Wsri.Restriction_Status'
            And LL.Type=9502
Where A.Alert_Key=121234 
    And Eri.Attack_Case_Id='Pp-123-0980'