您可以使用and子句对数据进行反透视以获得结果:CROSS APPLY
VALUES
select d.ContactMethod, d.id, d.Value1, d.Value2, d.Value3
from contacts c
left join address a
on c.addressid = a.id
left join email e
on c.emailid = e.id
left join phone p
on c.phoneid = p.id
cross apply
(
values
('Address', c.addressid, a.Line1, a.City, a.State),
('Email', c.emailid, e.eAddress, '', ''),
('Phone', c.phoneid, p.number, cast(p.extension as varchar(10)), '')
) d (ContactMethod, id, Value1, Value2, Value3)
请参阅SQL Fiddle with Demo。
这给出了结果:
| CONTACTMETHOD | ID | VALUE1 | VALUE2 | VALUE3 |
-----------------------------------------------------
| Address | 2 | N5980 | Onalaska | WI |
| Email | 8 | myEmail@ | | |
| Phone | 5 | 555-5555 | 1234 | |
如果您想要第二个结果,那么您可以使用多个连接来获得它:
select cm.ContactMethod,
a.id addressid,
a.line1,
a.city,
a.state,
e.id emailid,
e.eaddress,
p.id phoneid,
p.number,
p.extension
from contacts c
cross join
(
VALUES ('Address'),('Email'),('Phone')
) cm (ContactMethod)
left join address a
on c.addressid = a.id
and cm.ContactMethod = 'Address'
left join email e
on c.emailid = e.id
and cm.ContactMethod = 'Email'
left join phone p
on c.phoneid = p.id
and cm.ContactMethod = 'Phone';
请参阅SQL Fiddle with Demo。结果是:
| CONTACTMETHOD | ADDRESSID | LINE1 | CITY | STATE | EMAILID | EADDRESS | PHONEID | NUMBER | EXTENSION |
----------------------------------------------------------------------------------------------------------------
| Address | 2 | N5980 | Onalaska | WI | (null) | (null) | (null) | (null) | (null) |
| Email | (null) | (null) | (null) | (null) | 8 | myEmail@ | (null) | (null) | (null) |
| Phone | (null) | (null) | (null) | (null) | (null) | (null) | 5 | 555-5555 | 1234 |
编辑 #1,根据您的更改,您可以将查询更改为以下内容。
第一个包含三value
列,然后您可以添加一个WHERE
子句来过滤掉任何null
值:
select c.ID, ContactMethod, Value1, Value2, Value3
from contacts c
left join address a
on c.addressid = a.id
left join email e
on c.emailid = e.id
left join phone p
on c.phoneid = p.id
cross apply
(
values
('Address', c.addressid, a.Line1, a.City, a.State),
('Email', c.emailid, e.eAddress, null, null),
('Phone', c.phoneid, p.number, cast(p.extension as varchar(10)), null)
) d (ContactMethod, id, Value1, Value2, Value3)
where value1 is not null
or value2 is not null
or value3 is not null
请参阅SQL Fiddle with Demo。结果是:
ID | CONTACTMETHOD | VALUE1 | VALUE2 | VALUE3 |
---------------------------------------------------------------
| 1 | Address | N5980 | Onalaska | WI |
| 2 | Email | myEmail@ | (null) | (null) |
| 3 | Phone | 555-5555 | 1234 | (null) |
| 4 | Address | 1417 Saint Andrew | La Crosse | WI |
如果您希望结果在一行中,那么您将需要使用该UNPIVOT
函数:
select *
from
(
select id,
case col
when 'addressid' then 'address'
when 'emailid' then 'email'
when 'phoneid' then 'phone' end ContactMethod,
contact_id
from contacts
unpivot
(
contact_id
for col in (addressid, emailid, phoneid)
) unpiv
) c
left join address a
on c.contact_id = a.id
and c.ContactMethod = 'Address'
left join email e
on c.contact_id = e.id
and c.ContactMethod = 'Email'
left join phone p
on c.contact_id = p.id
and c.ContactMethod = 'Phone';
请参阅SQL Fiddle with Demo。这个查询的结果是:
| ID | CONTACTMETHOD | CONTACT_ID | LINE1 | CITY | STATE | EADDRESS | NUMBER | EXTENSION |
--------------------------------------------------------------------------------------------------------------
| 1 | address | 2 | N5980 | Onalaska | WI | (null) | (null) | (null) |
| 2 | email | 8 | (null) | (null) | (null) | myEmail@ | (null) | (null) |
| 3 | phone | 5 | (null) | (null) | (null) | (null) | 555-5555 | 1234 |
| 4 | address | 3 | 1417 Saint Andrew | La Crosse | WI | (null) | (null) | (null) |