SQL 查询联接表
SQL Query Join Tables
我正在尝试 join
基于地址 1 的 primary key
的表格。但是,下面的问题会为同一地址创建重复记录。 003 78057911 (wpphtp !=F) 和 003 78057922 (wpphtp = F) 是 database
中的相同字段,但我想将其提取到不同的列中。我该怎么做?
select ABAN8,'CREATE',ABALPH, '','', '','','','','','', EAEMAL,'',
CASE
WHEN wpphtp != 'F'
THEN WPAR1
else ' '
END AS prefix ,'', CASE
WHEN wpphtp != 'F'
THEN WPph1
else ' '
END AS phone, '',CASE
WHEN wpphtp = 'F'
THEN wpar1
else ' '
END AS prefixfax,' ',CASE
WHEN wpphtp = 'F'
THEN wpph1
else ' '
END AS fax from PRODDTA.F0111
join PRODDTA.F01151 on proddta.f0111.wwan8 = PRODDTA.F01151.EAAN8
join PRODDTA.F0115 JOIN PRODDTA.F0101 ON PRODDTA.F0115.wpAN8 = PRODDTA.F0101.ABAN8
ON PRODDTA.F0115.wpAN8 = PRODDTA.F0111.wwAN8 where wwidln < 1
order by wwan8
结果:
1 | CREATE | E-GLOBAL INNOVATIVE SDN BHD | kinki@hostpro2u.com | 003 | 78057911
1 | CREATE | E-GLOBAL INNOVATIVE SDN BHD | billing@hostpro2u.com | 003 | 78057911
1 | CREATE | E-GLOBAL INNOVATIVE SDN BHD | kinki@hostpro2u.com | 003 | 78057922
1 | CREATE | E-GLOBAL INNOVATIVE SDN BHD | billing@hostpro2u.com | 003 | 78057922
预期结果:
1 CREATE | E-GLOBAL INNOVATIVE SDN BHD | kinki@hostpro2u.com | 003 | 78057911 | 003 | 78057922
1 CREATE | E-GLOBAL INNOVATIVE SDN BHD | billing@hostpro2u.com | 003 | 78057911 | 003 | 78057922
您可以使用条件聚合来获取不同列中不同 wpphtp
的值。没有看到 table 结构和样本数据,很难 100% 确定,但这应该有效:
select ABAN8,'CREATE',ABALPH, '','', '','','','','','', EAEMAL,'',
MAX(CASE WHEN wpphtp != 'F' THEN WPAR1 END) AS prefix,'',
MAX(CASE WHEN wpphtp != 'F' THEN WPph1 END) AS phone, '',
MAX(CASE WHEN wpphtp = 'F' THEN wpar1 END) AS prefixfax,' ',
MAX(CASE WHEN wpphtp = 'F' THEN wpph1 END) AS fax
from PRODDTA.F0111
join PRODDTA.F01151 on proddta.f0111.wwan8 = PRODDTA.F01151.EAAN8
join PRODDTA.F0115 JOIN PRODDTA.F0101 ON PRODDTA.F0115.wpAN8 = PRODDTA.F0101.ABAN8
ON PRODDTA.F0115.wpAN8 = PRODDTA.F0111.wwAN8 where wwidln < 1
GROUP BY -- add all the other column names here
order by wwan8
在 GROUP BY
子句中,您需要添加 all 其他列名称(即除 prefix
、phone
、prefixfax
和 fax
).
我正在尝试 join
基于地址 1 的 primary key
的表格。但是,下面的问题会为同一地址创建重复记录。 003 78057911 (wpphtp !=F) 和 003 78057922 (wpphtp = F) 是 database
中的相同字段,但我想将其提取到不同的列中。我该怎么做?
select ABAN8,'CREATE',ABALPH, '','', '','','','','','', EAEMAL,'',
CASE
WHEN wpphtp != 'F'
THEN WPAR1
else ' '
END AS prefix ,'', CASE
WHEN wpphtp != 'F'
THEN WPph1
else ' '
END AS phone, '',CASE
WHEN wpphtp = 'F'
THEN wpar1
else ' '
END AS prefixfax,' ',CASE
WHEN wpphtp = 'F'
THEN wpph1
else ' '
END AS fax from PRODDTA.F0111
join PRODDTA.F01151 on proddta.f0111.wwan8 = PRODDTA.F01151.EAAN8
join PRODDTA.F0115 JOIN PRODDTA.F0101 ON PRODDTA.F0115.wpAN8 = PRODDTA.F0101.ABAN8
ON PRODDTA.F0115.wpAN8 = PRODDTA.F0111.wwAN8 where wwidln < 1
order by wwan8
结果:
1 | CREATE | E-GLOBAL INNOVATIVE SDN BHD | kinki@hostpro2u.com | 003 | 78057911
1 | CREATE | E-GLOBAL INNOVATIVE SDN BHD | billing@hostpro2u.com | 003 | 78057911
1 | CREATE | E-GLOBAL INNOVATIVE SDN BHD | kinki@hostpro2u.com | 003 | 78057922
1 | CREATE | E-GLOBAL INNOVATIVE SDN BHD | billing@hostpro2u.com | 003 | 78057922
预期结果:
1 CREATE | E-GLOBAL INNOVATIVE SDN BHD | kinki@hostpro2u.com | 003 | 78057911 | 003 | 78057922
1 CREATE | E-GLOBAL INNOVATIVE SDN BHD | billing@hostpro2u.com | 003 | 78057911 | 003 | 78057922
您可以使用条件聚合来获取不同列中不同 wpphtp
的值。没有看到 table 结构和样本数据,很难 100% 确定,但这应该有效:
select ABAN8,'CREATE',ABALPH, '','', '','','','','','', EAEMAL,'',
MAX(CASE WHEN wpphtp != 'F' THEN WPAR1 END) AS prefix,'',
MAX(CASE WHEN wpphtp != 'F' THEN WPph1 END) AS phone, '',
MAX(CASE WHEN wpphtp = 'F' THEN wpar1 END) AS prefixfax,' ',
MAX(CASE WHEN wpphtp = 'F' THEN wpph1 END) AS fax
from PRODDTA.F0111
join PRODDTA.F01151 on proddta.f0111.wwan8 = PRODDTA.F01151.EAAN8
join PRODDTA.F0115 JOIN PRODDTA.F0101 ON PRODDTA.F0115.wpAN8 = PRODDTA.F0101.ABAN8
ON PRODDTA.F0115.wpAN8 = PRODDTA.F0111.wwAN8 where wwidln < 1
GROUP BY -- add all the other column names here
order by wwan8
在 GROUP BY
子句中,您需要添加 all 其他列名称(即除 prefix
、phone
、prefixfax
和 fax
).