2017-06-22 62 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 

在连接中,两个查询都有所不同。

合并不会影响记录计数