数据以将联系人导出到不同的列

时间:2018-09-28 14:36:18

标签: sql data-export

我有2个表,一个表存储零售商信息(商店名称,地址,邮政编码),然后有一个联系人表,其中存储个人详细信息姓名,电话,电子邮件地址。

我正在导出数据,但是因为我可以为一个零售商拥有多个联系人,所以导出将为每个联系人/零售商复制行。

例如:
另一位零售商,安娜·特里菲(Anna Triffo),伦敦,石灰街26号,0207 4932678,anna @ another.com
另一个零售商,位于伦敦莱姆街26号,简·霍尔(Jane Hall),

我想为每个联系地点放在单独的列中,即: 零售商名称,交易名称,地址行1,地址行2,地址行3,地址行4,镇,国家/地区,联系人1,联系人2,联系人3,联系人4,电话1,电话2,电话3,电话4,电子邮件1电子邮件2,电子邮件3

因此,上面的示例将如下所示:
另一位零售商,伦敦莱姆街26号,安娜·特里菲(Anna Triffo),0207 4932678,anna @ another.com,简·霍尔(Jane Hall),

输出将是:
HeadOffice_YN,HeadOfficeId_N_R,ReferenceId,RetailerName,TradingName AddressLine1,AddressLine2,Contact1,Tel1,Email1,Contact2,Tel2,Email2,IBAN,BIC Rebate,RebateCalc,VATRegNo

和我的选择:

SELECT 
OL_HO_YN as 'HeadOffice_YN', 
SageX3ID as 'HeadOfficeId_N_R', 
tblBusinessPartner.BP_ID as 'ReferenceId', 
BP_NAME,
OL_NAME as 'RetailerName', 
OL_FULL_NAME as 'TradingName',
 @num_street_lines := 1 + LENGTH(OL_ADDRESS) - LENGTH(REPLACE(OL_ADDRESS, '\n', '')) AS num_street_lines,
    SUBSTRING_INDEX(OL_ADDRESS, '\n', 1) AS AddressLine1,
    IF(@num_street_lines > 1, SUBSTRING_INDEX(SUBSTRING_INDEX(OL_ADDRESS, '\n', 2), '\n', -1), '') AS AddressLine2,
    IF(@num_street_lines > 2, SUBSTRING_INDEX(SUBSTRING_INDEX(OL_ADDRESS, '\n', 3), '\n', -1), '') AS AddressLine3,
    RetailerCountry.CO_NAME as 'Country',
    if(OL_TOWN Is Null, '', OL_TOWN) AS 'Town', 
    if(BP_CREATED_DATE is null,'',BP_CREATED_DATE) as 'DateJoined_D',
    if(Contact.LastName is Null, Contact.FirstName, concat(Contact.FirstName, ' ', Contact.LastName)) as Contact1,
    Contact.TelNo as 'Tel1' , Contact.EmailAddress as 'Email1',
    #Add More Contact Details here
    BP_IBAN as 'IBAN', 
    BP_BIC as 'BIC',
    BP_REBATE as 'Rebate',
    tblCheckCodes.CC_Description as 'RebateCalc',
    OL_VAT_REG_NO as 'VATRegNo'

 FROM gbtfco_GBTF003L.tblBusinessPartner
join tblOutlet on BP_ID = OL_HO_BP_ID
join tblTerminal on OL_ID = TE_OL_ID
join tblCountry RetailerCountry on OL_CO_ID = CO_ID
join tblCheckCodes on tblBusinessPartner.BP_REBATE_CALC = tblCheckCodes.CC_CheckCode
left join ContactDetails Contact on tblOutlet.OL_ID = Contact.OL_ID 

谢谢
达伦

0 个答案:

没有答案