gpt4 book ai didi

sql - 根据 T-SQL 中的源表填充列数据

转载 作者:行者123 更新时间:2023-12-02 07:36:55 25 4
gpt4 key购买 nike

我什至不确定我的问题措辞是否正确,但这里是。

我有一个名为 Contacts 的表,它具有对表 Address、Email、Phone 的 FK 引用(这些有 1 对多的联系人)。我需要创建一个查询来提取所有数据,并有一个名为 Contact Method 的列,该列显示该行来自哪个子表。

Contact: ID, AddressID, EmailID, PhoneID
Address: ID, Line1, City, State
Email : ID, EAddress
Phone : ID, Number, Extension

我需要结果表看起来像这样:

ContactMethod | ID | [Value1] | [Value2] | [Value3]

Address 2 N5980 Onalaska WI
Email 8 myEmail@
Phone 5 555-5555 1234

或者,如果更简单的话,它可以在一行中列出所有组合的列,我也可以使用它。即

ContactMethad | ID | Line1 | City | State | ID | EAddress | ID | Number | Extension

我看了PIVOT ,这很简洁,但似乎并不能单独解决我的问题。我需要将它与 COALESCE 结合使用吗? ?

感谢您的帮助。


编辑

我在表格 Contact 上的数据如下所示:

ID | AddressID | PhoneID | EmailID

1 3 null null
2 null null 7
3 null 5 null
4 4 null null
5 null 6 null

建议的解决方案有效,但我为每个 ID 获取 3 行。有道理吗?

最佳答案

您可以使用 CROSS APPLYVALUES 子句反透视数据以获得结果:

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) |

关于sql - 根据 T-SQL 中的源表填充列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15418548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com