左连接产生错误的数据

时间:2018-06-06 16:20:12

标签: sql-server left-join

我正在加入gtwo表:Contacts,contacttype。联系人表包含联系人详细信息,联系人类型表包含联系人类型详细信息。

以下是联系表:

enter image description here

联系人类型表:

enter image description here

一个客户的所需输出:

enter image description here

我在查询中使用的左连接产生了错误的结果。我在这里添加了我的rexter链接。任何帮助将不胜感激!

Rexter链接: http://rextester.com/live/QGMAD33217

***新的Rexter链接(无实时编辑:http://rextester.com/KSP51539

查询:

create table contacttype(ctype_id int,ctype varchar(20))
insert contacttype values
( 12    ,'Ctype1'),
( 13    ,'Ctype2'),
( 14    ,'Ctype3')

create table contacts(cid int,ctype_id int,name varchar(20), phone varchar(15))
insert contacts values
(1001,  12  ,'Tim', 1234567890),
(1001,  13  ,'Joe', 9874563210),
(1001,  14  ,'Jack',    6547893214),
(1002,  12  ,'Jane',    6547896125),
(1002,  13  ,'Smith',   null),
(1002,  14  ,'Jill',    9878445623 )

select c.cid,
       max(case when ct.ctype = 'Ctype1' then c.name end) as [ctype1_name],
       c1.phone,
       max(case when ct.ctype = 'Ctype2' then c.name end) as [ctype2_name],
       c2.phone,
       max(case when ct.ctype = 'Ctype3' then c.name end) as [ctype3_name],
       c3.phone
from contacts c
join contacttype ct on c.ctype_id = ct.ctype_id
left join contacts c1 on c1.ctype_id = ct.ctype_id and ct.ctype = 'Ctype1'
left join contacts c2 on c2.ctype_id = ct.ctype_id and ct.ctype = 'Ctype2'
left join contacts c3 on c3.ctype_id = ct.ctype_id and ct.ctype = 'Ctype3'
group by c.cid,c1.phone,c2.phone,c3.phone

2 个答案:

答案 0 :(得分:2)

既然你有一个稳定的链接,很容易看出出了什么问题。你有条件聚合但它不是你想要的方式。这应该产生您正在寻找的输出。

select c.cid
    , ctype1_name = max(case when ct.ctype = 'Ctype1' then c.name end) 
    , phone1 = max(case when ct.ctype = 'Ctype1' then c.phone end) 
    , ctype2_name = max(case when ct.ctype = 'Ctype2' then c.name end) 
    , phone2 = max(case when ct.ctype = 'Ctype2' then c.phone end) 
    , ctype3_name = max(case when ct.ctype = 'Ctype3' then c.name end) 
    , phone3 = max(case when ct.ctype = 'Ctype3' then c.phone end) 
from contacts c
join contacttype ct on c.ctype_id = ct.ctype_id
group by c.cid

答案 1 :(得分:1)

或者,如果预定义了ctype_id的值,则可以使用数据关系运算符

declare @contacttype table (ctype_id int,ctype varchar(20))
insert @contacttype values
( 12    ,'Ctype1'),
( 13    ,'Ctype2'),
( 14    ,'Ctype3');

declare @contacts table (cid int,ctype_id int,name varchar(20), phone varchar(15))
insert @contacts values
(1001,  12  ,'Tim', 1234567890),
(1001,  13  ,'Joe', 9874563210),
(1001,  14  ,'Jack',    6547893214),
(1002,  12  ,'Jane',    6547896125),
(1002,  13  ,'Smith',   null),
(1002,  14  ,'Jill',    9878445623 );

with A as (
    select 
        cid, [12] as ctype1_name, [13] as ctype2_name, [14] as ctype3_name
    from
        (select cid, ctype_id, name from @contacts) p pivot (max(name) for ctype_id in ([12],[13],[14])) as pvt
), B as (
    select 
        cid, [12] as phone1, [13] as phone2, [14] as phone3
    from
        (select cid, ctype_id, phone from @contacts) p pivot (max(phone) for ctype_id in ([12],[13],[14])) as pvt
)
select
    A.cid, A.ctype1_name, B.phone1, A.ctype2_name, B.phone2, A.ctype3_name, B.phone3
from
    A inner join B on (A.cid = B.cid)