0

我正在尝试join基于primary key地址编号 1 的表格。但是,下面的问题会为同一地址编号创建重复记录。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
4

1 回答 1

1

您可以使用条件聚合来获取wpphtp不同列中不同的值。如果没有看到表结构和示例数据,很难 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子句中,您需要添加所有其他列名(即除prefixphone和之外prefixfax的所有内容fax)。

于 2019-12-04T08:19:38.907 回答