2015-05-21 81 views
4

讓一個簡單的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 
+0

你看,看看哪些記錄不是最終的結果?那裏有一種模式嗎? –

+0

在黑暗中拍攝。但嘗試給第二個查詢一個不同的別名。另外我想知道是否有其他查詢結果被緩存,這是什麼給你錯誤的結果。最後,該ROW_NUMBER()是可疑的。 – dotnetN00b

+0

我會取出ROW_NUMBER()並在第二個查詢中保留空值,以確保您獲取1222條記錄。 – FutbolFan

回答

1

你的查詢將返回表中的所有行。除非執行之間的表更改,否則分別運行子查詢的結果應與使用UNION ALL運行它們的結果相同。

作爲一個說明,如果你想簡化查詢,那麼你可以做:

SELECT COALESCE(a.WBS_ELEMENT_ID, 
       ROW_NUMBER() OVER (PARTITION BY wbs_element_id ORDER BY a. wbs_element_desc) 
       ) 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; 
+0

^這是票。雖然我結束了使用ISNULL函數,因爲我確實在該WBS_ELEMENT_ID列中的數據類型有一些差異。 COALESCE顯然會踢出任何不是相同數據類型的東西。 – user2891330

+0

確實,'ISNULL()'和'COALESCE()'可以有不同的返回類型。有趣的是,這實際上影響你的應用程序。這裏是關於這個主題的文章:http://sqlmag.com/t-sql/coalesce-vs-isnull。 –

0

顯然你的語法沒有問題,但是如果你想嘗試一種不同的方法來讓你的UNION ALLROW_NUMBER一起工作。這裏是:

;WITH q1 
AS (
    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 b.WBS_ELEMENT_ID AS [WBS Element] --just bring NULL values 
     ,b.WBS_ELEMENT_DESC AS [WBS Element name] 
     ,b.UHC_INDUSTRY AS [Industry] 
     ,b.UHC_SECTOR AS [Sector] 
     ,b.UHC_DUNS_NUMBER AS [UHC DUNS Number] 
     ,b.UHC_DUNS_NAME AS [UHC DUNS Name] 
     ,b.PRIORITY_SUB_SECTOR AS [Priority Sub Sector] 
     ,b.BUDGET_ALLOCATION AS [Budget Allocation] 
     ,b.LAST_UPDATED_ON AS [Last Updated] 
    FROM dimsectorpd b 
    WHERE b.WBS_ELEMENT_ID IS NULL 
    ) 
SELECT CASE 
     WHEN q1.[WBS Element] IS NULL 
      THEN ROW_NUMBER() OVER (ORDER BY q1.WBS_Element_Desc) 
     ELSE q1.[WBS Element] 
     END [WBS_Element] 
    ,q1.[WBS Element Desc] 
    ,q1.Industry 
    ,q1.Sector 
    ,q1.[UHC DUNS Number] 
    ,q1.[UHC DUNS Name] 
    ,q1.[Priority Sub Sector] 
    ,q1.[Budget Allocation] 
    ,q1.[Last Updated] 
FROM q1 
0

這裏是一個簡化的例子,你可以看看它是否可以在你的服務器上工作嗎?

SELECT a.low AS [My ID], 
    a.name AS [My Letter] 
FROM master..spt_values as a 
WHERE low is not null 

UNION ALL 

SELECT ROW_NUMBER() OVER (ORDER BY a.name) AS [My ID], 
    a.name AS [My Letter] 
FROM master..spt_values as a 
WHERE a.low is null 

master..spt_values爲2515行我的系統上...