SQL - 替代UNION - JOIN可能吗?

时间:2017-11-17 21:06:11

标签: sql sql-server join union

此查询效果不佳。我想知道这是否可以在更少的CTE和/或没有UNION的情况下完成。

WITH SourceOutputNodes
              AS (
              SELECT t.p.value('@OutputNodeID', 'uniqueidentifier') AS OutputNodeID,
                     t.p.value('@OutputNodeTypeID', 'int') AS OutputNodeTypeID,
                     t.p.value('@OutputTemplateID', 'uniqueidentifier') AS OutputTemplateID,
                     t.p.value('@OutputTemplateBookmarkID', 'uniqueidentifier') AS OutputTemplateBookmarkID,
                     t.p.value('@PublicNodeID', 'uniqueidentifier') AS PublicNodeID,
                     t.p.value('@Name', 'nvarchar(100)') AS Name,
                     t.p.value('@Description', 'nvarchar(300)') AS [Description],
                     t.p.value('@CreatedDate', 'datetime') AS CreatedDate,
                     coalesce(t.p.value('@CreatedByUserID', 'uniqueidentifier'), '00000000-0000-0000-0000-000000000000') AS CreatedByUserID,
                     t.p.value('@DeletedDate', 'datetime') AS DeletedDate,
                     t.p.value('@DeletedByUserID', 'uniqueidentifier') AS DeletedByUserID,
                     t.p.value('@ModifiedByUserID', 'uniqueidentifier') AS ModifiedByUserID,
                     t.p.value('@ModifiedDate', 'datetime') AS ModifiedDate,
                     t.p.value('@IsPublic', 'bit') AS IsPublic,
                     t.p.value('@DisplayOrder', 'int') AS DisplayOrder,
                     t.p.value('@RuleID', 'uniqueidentifier') AS RuleID,
                     t.p.value('@RuleDescription', 'nvarchar(max)') AS RuleDescription,
                     t.p.value('@DataItemID', 'uniqueidentifier') AS DataItemID,
                     t.p.value('@DataItemName', 'nvarchar(100)') AS DataItemName,
                     t.p.value('@DataItemOtherOptions', 'nvarchar(max)') AS DataItemOtherOptions,
                     t.p.value('@OutputDocRepeaterOptionID', 'int') AS OutputDocRepeaterOptionID,
                     t.p.value('@FormOptions', 'nvarchar(max)') AS FormOptions,
                     t.p.value('@FormID', 'uniqueidentifier') AS FormID,
                     t.p.value('@DefaultValue', 'nvarchar(max)') AS DefaultValue,
                     t.p.value('@GenericFieldDefinitionInspectionTypeID', 'uniqueidentifier') AS GenericFieldDefinitionInspectionTypeID,
                     t.p.value('@GenericFieldDefinitionRoleID', 'uniqueidentifier') AS GenericFieldDefinitionRoleID,
                     t.p.value('@PhotoGrid_ImagesToShow', 'nvarchar(50)') AS PhotoGrid_ImagesToShow,
                     t.p.value('@PhotoGrid_NumberColumns', 'int') AS PhotoGrid_NumberColumns,
                     t.p.value('@PhotoGrid_NumberRows', 'int') AS PhotoGrid_NumberRows,
                     t.p.value('@Photo_PublicOnly', 'bit') AS Photo_PublicOnly,
                     t.p.value('@Photo_LabelPosition', 'nvarchar(50)') AS Photo_LabelPosition,
                     t.p.value('@Photo_Number', 'int') AS Photo_Number,
                     t.p.value('@LinkedToNodeID', 'uniqueidentifier') AS LinkedToNodeID,
                     t.p.value('@UseNoPhotoPlaceholder', 'bit') AS UseNoPhotoPlaceholder,
                     t.p.value('@NoPhotoPlaceholderURL', 'nvarchar(max)') AS NoPhotoPlaceholderURL,
                     t.p.value('@Photo_HighlightHazards', 'bit') AS Photo_HighlightHazards,
                     t.p.value('@Photo_Level', 'int') AS Photo_Level,
                     t.p.value('@CommCov_Level', 'int') AS CommCov_Level
              FROM [Promotion].Promotions AS p
                   CROSS APPLY p.OutputsXml.nodes('/Data/Outputs.OutputNodes/Outputs.OutputNodes') t(p)
              WHERE p.PromotionID = 'xxxxx'),--@promotionID),

  OutputNodesNullBookmarks AS (
   SELECT * FROM SourceOutputNodes WHERE OutputTemplateBookmarkID IS NULL
  ),

  OutputNodesMatchingBookmarks AS (
SELECT o.* 
FROM SourceOutputNodes o
INNER JOIN [Outputs].[OutputDocTemplateBookmarks] b
ON o.OutputTemplateBookmarkID = b.[OutputDocTemplateBookmarkID]
  ),

  OutputNodes AS (
   SELECT * 
   FROM 
   OutputNodesNullBookmarks
   UNION 
   SELECT *
   FROM
   OutputNodesMatchingBookmarks
  )
  SELECT * FROM OutputNodes

1 个答案:

答案 0 :(得分:0)

我不确定第一个CTE,但第二个,第三个和第四个可以与WHERE子句中的OR结合使用相关子查询。

WITH SourceOutputNodes
AS (
    SELECT t.p.value('@OutputNodeID', 'uniqueidentifier') AS OutputNodeID,
        t.p.value('@OutputNodeTypeID', 'int') AS OutputNodeTypeID,
        t.p.value('@OutputTemplateID', 'uniqueidentifier') AS OutputTemplateID,
        t.p.value('@OutputTemplateBookmarkID', 'uniqueidentifier') AS OutputTemplateBookmarkID,
        t.p.value('@PublicNodeID', 'uniqueidentifier') AS PublicNodeID,
        t.p.value('@Name', 'nvarchar(100)') AS NAME,
        t.p.value('@Description', 'nvarchar(300)') AS [Description],
        t.p.value('@CreatedDate', 'datetime') AS CreatedDate,
        coalesce(t.p.value('@CreatedByUserID', 'uniqueidentifier'), '00000000-0000-0000-0000-000000000000') AS CreatedByUserID,
        t.p.value('@DeletedDate', 'datetime') AS DeletedDate,
        t.p.value('@DeletedByUserID', 'uniqueidentifier') AS DeletedByUserID,
        t.p.value('@ModifiedByUserID', 'uniqueidentifier') AS ModifiedByUserID,
        t.p.value('@ModifiedDate', 'datetime') AS ModifiedDate,
        t.p.value('@IsPublic', 'bit') AS IsPublic,
        t.p.value('@DisplayOrder', 'int') AS DisplayOrder,
        t.p.value('@RuleID', 'uniqueidentifier') AS RuleID,
        t.p.value('@RuleDescription', 'nvarchar(max)') AS RuleDescription,
        t.p.value('@DataItemID', 'uniqueidentifier') AS DataItemID,
        t.p.value('@DataItemName', 'nvarchar(100)') AS DataItemName,
        t.p.value('@DataItemOtherOptions', 'nvarchar(max)') AS DataItemOtherOptions,
        t.p.value('@OutputDocRepeaterOptionID', 'int') AS OutputDocRepeaterOptionID,
        t.p.value('@FormOptions', 'nvarchar(max)') AS FormOptions,
        t.p.value('@FormID', 'uniqueidentifier') AS FormID,
        t.p.value('@DefaultValue', 'nvarchar(max)') AS DefaultValue,
        t.p.value('@GenericFieldDefinitionInspectionTypeID', 'uniqueidentifier') AS GenericFieldDefinitionInspectionTypeID,
        t.p.value('@GenericFieldDefinitionRoleID', 'uniqueidentifier') AS GenericFieldDefinitionRoleID,
        t.p.value('@PhotoGrid_ImagesToShow', 'nvarchar(50)') AS PhotoGrid_ImagesToShow,
        t.p.value('@PhotoGrid_NumberColumns', 'int') AS PhotoGrid_NumberColumns,
        t.p.value('@PhotoGrid_NumberRows', 'int') AS PhotoGrid_NumberRows,
        t.p.value('@Photo_PublicOnly', 'bit') AS Photo_PublicOnly,
        t.p.value('@Photo_LabelPosition', 'nvarchar(50)') AS Photo_LabelPosition,
        t.p.value('@Photo_Number', 'int') AS Photo_Number,
        t.p.value('@LinkedToNodeID', 'uniqueidentifier') AS LinkedToNodeID,
        t.p.value('@UseNoPhotoPlaceholder', 'bit') AS UseNoPhotoPlaceholder,
        t.p.value('@NoPhotoPlaceholderURL', 'nvarchar(max)') AS NoPhotoPlaceholderURL,
        t.p.value('@Photo_HighlightHazards', 'bit') AS Photo_HighlightHazards,
        t.p.value('@Photo_Level', 'int') AS Photo_Level,
        t.p.value('@CommCov_Level', 'int') AS CommCov_Level
    FROM [Promotion].Promotions AS p
    CROSS APPLY p.OutputsXml.nodes('/Data/Outputs.OutputNodes/Outputs.OutputNodes') t(p)
    WHERE p.PromotionID = 'xxxxx'
    ), --@promotionID)
SELECT *
FROM SourceOutputNodes o
WHERE OutputTemplateBookmarkID IS NULL
    OR EXISTS (SELECT [OutputDocTemplateBookmarkID] FROM [Outputs].[OutputDocTemplateBookmarks] WHERE o.OutputTemplateBookmarkID = OutputDocTemplateBookmarkID)