讓一個簡單的UNION ALL查詢結合兩個查詢的結果。第一個查詢,獨立運行,返回1208個記錄,第二個14我希望所有正確syntaxed UNION返回1222條記錄,但礦下降到896T-SQL [UNION ALL]從查詢結果中刪除記錄
使零意義上對我說:
SELECT a.WBS_ELEMENT_ID as [WBS Element],
a.WBS_ELEMENT_DESC as [WBS Element Desc],
a.UHC_INDUSTRY as [Industry],
a.UHC_SECTOR as [Sector],
a.UHC_DUNS_NUMBER as [UHC DUNS Number],
a.UHC_DUNS_NAME as [UHC DUNS Name],
a.PRIORITY_SUB_SECTOR as [Priority Sub Sector],
a.BUDGET_ALLOCATION as [Budget Allocation],
a.LAST_UPDATED_ON as [Last Updated]
FROM DimSectorPd a
WHERE a.wbs_element_id is not null
UNION ALL
SELECT ROW_NUMBER() OVER (ORDER BY a.wbs_element_desc) as [WBS Element],
a.WBS_ELEMENT_DESC as [WBS Element name],
a.UHC_INDUSTRY as [Industry],
a.UHC_SECTOR as [Sector],
a.UHC_DUNS_NUMBER as [UHC DUNS Number],
a.UHC_DUNS_NAME as [UHC DUNS Name],
a.PRIORITY_SUB_SECTOR as [Priority Sub Sector],
a.BUDGET_ALLOCATION as [Budget Allocation],
a.LAST_UPDATED_ON as [Last Updated]
from dimsectorpd a where a.WBS_ELEMENT_ID is null
你看,看看哪些記錄不是最終的結果?那裏有一種模式嗎? –
在黑暗中拍攝。但嘗試給第二個查詢一個不同的別名。另外我想知道是否有其他查詢結果被緩存,這是什麼給你錯誤的結果。最後,該ROW_NUMBER()是可疑的。 – dotnetN00b
我會取出ROW_NUMBER()並在第二個查詢中保留空值,以確保您獲取1222條記錄。 – FutbolFan