2017-06-22 61 views
0

我在下面有兩個查詢。第一個查詢返回83行,這是我所期望的。查詢2雖然返回166行(所以第一個查詢是雙倍)。唯一不同的是在兩個PF和PC的select語句如下圖所示,使用coalesce雙倍記錄數返回

查詢1 SEDOL

查詢2 合併(ISIN,SEDOL,BbergTicker)身份證

我不明白爲什麼這會使返回的行數增加一倍?

查詢1

;with pf as 
(
    select Name, Sedol, Nominal, FundCode, FileCode 
    from tbl1L where FundCode = 'BUNM' and CashItem = 0 and FileCode in ('MAIN', 'REISTD') 
), pc as 
(
    select Name, Sedol, Nominal, FundCode, FileCode 
from tbl1C where FundCode = 'BUNM' and CashItem = 0 and FileCode in ('MAIN', 'REDIST') 
) 
select coalesce(pf.FundCode, pc.FundCode) Fund, coalesce(pf.FileCode, pc.FileCode) FileCode, 
coalesce(pf.Name, pc.Name) Name, coalesce(pf.Sedol, pc.Sedol) Sedol, 
isnull(pf.Nominal, 0) PfNom, isnull(pc.Nominal, 0) PcNom, 
isnull(isnull(pf.Nominal, 0) - isnull(pc.Nominal, 0), -999) NomDiff 
from pf full outer join pc on pf.Sedol = pc.Sedol and pf.FileCode = pc.FileCode 
where isnull(isnull(pf.Nominal, 0) - isnull(pc.Nominal, 0),-999) <> 0 

查詢2

;with pf as 
(
    select Name, Coalesce(ISIN, Sedol, BbergTicker) Id, Nominal, FundCode, FileCode 
    from tbl1L where FundCode = 'BUNM' and CashItem = 0 and FileCode in ('MAIN', 'REISTD') 
), pc as 
(
    select Name, Coalesce(ISIN, Sedol, BbergTicker) Id, Nominal, FundCode, FileCode 
from tbl1C where FundCode = 'BUNM' and CashItem = 0 and FileCode in ('MAIN', 'REDIST') 
) 
select coalesce(pf.FundCode, pc.FundCode) Fund, coalesce(pf.FileCode, pc.FileCode) FileCode, 
coalesce(pf.Name, pc.Name) Name, coalesce(pf.Id, pc.Id) Id, coalesce(pf.Sedol, pc.Sedol) Sedol, 
isnull(pf.Nominal, 0) PfNom, isnull(pc.Nominal, 0) PcNom, 
isnull(isnull(pf.Nominal, 0) - isnull(pc.Nominal, 0), -999) NomDiff 
from pf full outer join pc on pf.Id = pc.Id and pf.FileCode = pc.FileCode 
where isnull(isnull(pf.Nominal, 0) - isnull(pc.Nominal, 0),-999) <> 0 
+0

在第二個查詢,你仍然有這個'COALESCE(pf.Sedol,pc.Sedol)',但沒有熱膨脹係數的直接上面有一列輸出稱爲' sedol'。你的代碼是所有的代碼嗎,還是你爲我們設計了格式? – SchmitzIT

+0

看看第二個例子中pf和pc的結果。他們必須生成重複的Id值。 – mjsqu

+0

不要害怕給你的查詢添加一些空白,所以它們不是文字的牆。正如這張貼幾乎無法閱讀。 –

回答

3

查詢1:

pf.Sedol = pc.Sedol and pf.FileCode = pc.FileCode 

查詢2:

pf.Id = pc.Id and pf.FileCode = pc.FileCode 

在連接中,兩個查詢都有所不同。

合併不會影響記錄計數