PROFESSIONAL表有很多列FIRST_NAME,MIDDLE_NAME,LAST_NAME,TITLE_CODE,COMMUNICATION_TYPE_CODE,COMMUNICATION_VALUE
PROFESSIONAL Table
COMMUNICATION_TYPE_CODE column
Telephone
Telephone
Fax
Fax
E-mail
E-mail
Cellular
Cellular
COMMUNICATION_VALUE column
224-256-5689
547-568-5642
254-565-4526
524-465-6542
jones@gmail.com
james@yahoo.com
562-564-7854
654-452-6897
这只是一个样本。 COMMUNICATION_TYPE_CODE和COMMUNICATION_VALUE列有500,00条这样的记录
Telephone = PHONE1
Cellular = PHONE2
E-mail = EMAIL1
Fax = FAX1
SELECT 'IMD Data Source' as DATA_SOURCE,
FIRST_NAME as FIRST_NM,
MIDDLE_NAME as MIDDLE_NM,
LAST_NAME as LAST_NM,
TITLE_CODE as CREDENTIALS,
PHONE1,
PHONE2,
EMAIL1,
FAX1,
FROM DLA_PROFESSIONAL_A
I already mapped the FIRST_NAME,MIDDLE_NAME, LAST_NAME, TITLE_CODE to target columns in my select statement so dont worry about that. I am having problems mapping PHONE1,PHONE2,EMAIL1,FAX1
当通讯类型代码为(电话,传真,电子邮件,移动电话)时,我必须选择通讯值列数据(电话号码,电子邮件,传真,手机)
这要进入4个单独的列(当COMMUNICATION_TYPE_CODE ='电话'时,我需要从列中提取COMMUNICATION_VALUE数据并将其映射到 在输出中PHONE1列,当COMMUNICATION_TYPE_CODE ='Cellular'时,我需要从列中提取COMMUNICATION_VALUE数据并将其映射到 在输出中PHONE2列,当COMMUNICATION_TYPE_CODE ='电子邮件'时,我需要从列中提取COMMUNICATION_VALUE数据并将其映射到 输出中的EMAIL1列,当COMMUNICATION_TYPE_CODE ='Fax'时,我需要从列中提取COMMUNICATION_VALUE数据并将其映射到 输出中的EMAIL1列)
I have the separate sequels I dont know how to put it in to the original select statement.
SELECT DISTINCT COMMUNICATION_VALUE FROM DG_SP_NP.DLA_PROFESSIONAL_A WHERE COMMUNICATION_TYPE_CODE='Telephone'
SELECT DISTINCT COMMUNICATION_VALUE FROM DG_SP_NP.DLA_PROFESSIONAL_A WHERE COMMUNICATION_TYPE_CODE='Cellular'
SELECT DISTINCT COMMUNICATION_VALUE FROM DG_SP_NP.DLA_PROFESSIONAL_A WHERE COMMUNICATION_TYPE_CODE='E-mail'
SELECT DISTINCT COMMUNICATION_VALUE FROM DG_SP_NP.DLA_PROFESSIONAL_A WHERE COMMUNICATION_TYPE_CODE='Fax'
我想做这样的事情。这不是4输出的正确sql,但你明白了。
SELECT 'IMD Data Source' DATA_SOURCE,
FIRST_NAME as FIRST_NM,
MIDDLE_NAME as MIDDLE_NM,
LAST_NAME as LAST_NM,
TITLE_CODE as CREDENTIALS,
SELECT DISTINCT COMMUNICATION_VALUE FROM PROFESSIONAL WHERE COMMUNICATION_TYPE_CODE='Telephone' as PHONE1,
SELECT DISTINCT COMMUNICATION_VALUE FROM PROFESSIONAL WHERE COMMUNICATION_TYPE_CODE='Cellular' as PHONE2,
SELECT DISTINCT COMMUNICATION_VALUE FROM PROFESSIONAL WHERE COMMUNICATION_TYPE_CODE='E-mail' as EMAIL1,
SELECT DISTINCT COMMUNICATION_VALUE FROM PROFESSIONAL WHERE COMMUNICATION_TYPE_CODE='Fax' as FAX1,
FROM PROFESSIONAL
所有数据都在一个表(PROFESSIONAL)
中答案 0 :(得分:1)
你在做什么是相当复杂的。您正在尝试单独订购联系信息。 SQL并不完全支持这一点。
相反,您需要添加一个“行号”列,在下面的代码中称为seqnum。查询使用此信息将联系人信息组合到适当的行。我发现union和group方法更容易(替代方法是完全外连接)。
select p.<whatever>, phone1, phone2, EmailContacts, fax
from Professional p left outer join
(select ProfessionalId, seqnum,
MAX(Phone1) as Phone1, MAX(Phone2) as Phone2, MAX(Email) as Email, MAX(Fax) as fax
from ((select ProfessionalId, COMMUNICATION_VALUE as Phone1,
NULL as Phone2, NULL as Email, NULL as Fax
row_number() over (partition by ProfessionalId order by ProfessionalId) as seqnum
from (select distinct ProfessionalId, COMMUNICATION_VALUE,
from Professional p
where COMMUNICATION_TYPE_CODE = 'Telephone'
)
) union all
(select ProfessionalId, NULL, COMMUNICATION_VALUE, NULL, NULL
row_number() over (partition by ProfessionalId order by ProfessionalId) as seqnum
from (select distinct ProfessionalId, COMMUNICATION_VALUE,
from Professional p
where COMMUNICATION_TYPE_CODE = 'Cellular'
)
) union all
(select ProfessionalId, NULL, NULL, COMMUNICATION_VALUE, NULL
row_number() over (partition by ProfessionalId order by ProfessionalId) as seqnum
from (select distinct ProfessionalId, COMMUNICATION_VALUE,
from Professional p
where COMMUNICATION_TYPE_CODE = 'E-mail'
)
) union all
(select ProfessionalId, NULL, NULL, NULL, COMMUNICATION_VALUE,
row_number() over (partition by ProfessionalId order by ProfessionalId) as seqnum
from (select distinct ProfessionalId, COMMUNICATION_VALUE,
from Professional p
where COMMUNICATION_TYPE_CODE = 'Fax'
)
)
)
group by ProfessionalId, seqnum
) c
on c.ProfessionalId = p.ProfessionalId
此查询假设每行都有一个ProfessionalId,用于标识每个人。
答案 1 :(得分:0)
那是不可能的。
我假设你有类似的东西:
JIM | SMITH | TELEPHONE | 224-256-5689
JIM | SMITH | TELEPHONE | 547-568-5642
单个(不同的)JIM SMITH行如何知道哪个communication_values在有多个时返回?
您要么获得&gt; 1行,要么您需要选择TOP 1或其他一些标准来返回您需要的单个值。
也许你想要这样的东西作为你的嵌套选择:
(SELECT TOP 1 COMMUNICATION_VALUE FROM PROFESSIONAL P_INNER WHERE COMMUNICATION_TYPE_CODE='Telephone' AND P_INNER.<field/s> = P_OUTER.<field/s> ORDER BY <some_field>)
修改强>
这样的事情?
CASE WHEN COMMUNICATION_TYPE_CODE='Telephone' THEN COMMUNICATION_VALUE END as PHONE1,
CASE WHEN COMMUNICATION_TYPE_CODE='Cellular' THEN COMMUNICATION_VALUE END as PHONE2,
CASE WHEN COMMUNICATION_TYPE_CODE='E-mail' THEN COMMUNICATION_VALUE END as EMAIL1,
CASE WHEN COMMUNICATION_TYPE_CODE='Fax' THEN COMMUNICATION_VALUE END as FAX1,
答案 2 :(得分:0)
关系表中的列只能包含一个值。这意味着,根据您提供的模式(列名称),PROFESSIONAL表的每个条目只能有一个用于COMMUNICATION_CODE的值和一个用于COMMUNICATION_VALUE的值。在关系数据库术语中,这被称为First Normal Form。
话虽如此,您可以在PROFESSIONAL表中有多个条目,每个条目都具有相同的名称信息,但具有唯一的COMMUNICATION_VALUE和COMMUNICAITON_CODE数据。但是,这违反了另一种关系设计考虑因素,即尽量减少数据重复。
我的建议是将COMMUNICATION_VALUE和COMMUNICATION_CODE放入一个单独的表中,然后使用PROFESSIONAL表的主键作为此新COMMUNICATION表的辅助键,以便能够为每个条目选择所有的COMMUNICATION TABLE值。专业表。
这有意义吗?