左外连接未返回预期的resultSet

时间:2016-09-12 23:55:18

标签: sql sql-server sql-server-2008 join sql-server-2012

我有这两个表

              #TABLE1#
##CompanyName##    ##PrimaryKey##
DELL                     1
DELL CALIFORNIA          2
DELL SAN FRANCISCO       3
DELL LOS ANGELES         4
IBM                      5
GOOGLE                   6

                                     #TABLE2#
##ParentComp##   ##ParentPrimaryKey##       ##ChildComp##    ##ChildPrimaryKey##
    DELL                     1             DELL CALIFORNIA            2
DELL CALIFORNIA              2             DELL SAN FRANCISCO         3
DELL CALIFORNIA              2             DELL LOS ANGELES           4

现在预期的表格如下所示;关系(层次结构)列只有三个可能的值(PARENT / CHILD / INDEPENDENT),如下所示:

                               #TABLE3#
##CompanyName##             ##Relationship##                     ##ParentCompany##
DELL                             PARENT                                ---
DELL CALIFORNIA                  CHILD                                 DELL
DELL SAN FRANCISCO               CHILD                             DELL CALIFORNIA
DELL LOS ANGELES                 CHILD                             DELL CALIFORNIA
IBM                           INDEPENDENT                              ---
GOOGLE                        INDEPENDENT                              ---

我已经尝试过以下两个加入选项

  

选择
  ...
  表1
  左外连接
  Table2 ON Table1.PrimaryKey = Table2.ParentPrimaryKey
  左外连接
  表2 tAlias2
  Table2.ParentPrimaryKey = tAlias2.ChildPrimaryKey

返回

 ##CompanyName##               ##Relationship##                     ##ParentCompany##
    DELL                             PARENT                                ---
    DELL CALIFORNIA                  CHILD                                 DELL
    DELL SAN FRANCISCO            INDEPENDENT                              ---
    DELL LOS ANGELES              INDEPENDENT                              ---
    IBM                           INDEPENDENT                              ---
    GOOGLE                        INDEPENDENT                              ---

  

选择
  ...
  表1
  左外连接
  Table2 ON Table1.PrimaryKey = Table2.ChildPrimaryKey
  左外连接
  表1 tAlias1
  Table2.ParentPrimaryKey = tAlias1.PrimaryKey

会返回这样的内容:

 ##CompanyName##               ##Relationship##                     ##ParentCompany##
    DELL                           INDEPENDENT                             ---
    DELL CALIFORNIA                  CHILD                                 DELL
    DELL SAN FRANCISCO               CHILD                              DELL CALIFORNIA
    DELL LOS ANGELES                 CHILD                              DELL CALIFORNIA
    IBM                            INDEPENDENT                             ---
    GOOGLE                         INDEPENDENT                             ---

2 个答案:

答案 0 :(得分:0)

加入talias2.ChildPrimaryKey后,您需要加入Table1.PrimaryKey列。

SELECT ..
FROM   table1 
       LEFT OUTER JOIN table2 
                    ON table1.primarykey = table2.parentprimarykey 
       LEFT OUTER JOIN table2 talias2 
                    ON table1.primarykey = talias2.childprimarykey --here

当您使用tAlias2.ChildPrimaryKey加入table2.parentprimarykey时,只有来自table2table1table1.primarykey = table2.parentprimarykey)的匹配记录才能加入tAlias2.ChildPrimaryKey

答案 1 :(得分:0)

您的架构显然是多余的。您可以删除table2并从以下内容中获得所需内容。

declare @tbl table (CompanyName varchar(100),PrimaryKey int primary key, parentId int)

insert @tbl values
('DELL',                     1,null),
('DELL CALIFORNIA',          2,1),
('DELL SAN FRANCISCO',       3,2),
('DELL LOS ANGELES',         4,2),
('IBM',                      5,null),
('google',                   6,null)


;with tbl as (
select PrimaryKey, CompanyName, parentId,
rel = cast(case when exists(select 1 from @tbl t1 where t1.parentId=t.PrimaryKey) then 'parent'
      else 'independent' end as varchar(100)),
cast(null as varchar(100)) parentName
from @tbl t
where t.parentId is null
union all
select t.PrimaryKey, t.CompanyName, t.parentId,cast('child' as varchar(100)),
tbl.CompanyName
from @tbl t inner join tbl on t.parentId=tbl.PrimaryKey
)
select * from tbl

order by PrimaryKey