我正在使用PostgreSQL客户记录 .
我的任务是导出客户记录 .
除了有联系信息的客户,我还有另一张 table .
每行一个联系人项目(电话, Value 或电子邮件, Value 等) .
当我加入并关联数据时,我会为每个客户ID提取多条记录(如果每个客户的通信类型超过1个,例如电话和电子邮件) .
我怎么能代替为每个通信类型创建另一行,将信息放入临时列,如电话列,传真列和电子邮件列 - 然后每个客户只有一行 .
edit -- you guys are masters
带列的数据库表:
account {
id
accountid
title (company name?)
shiptoaddress_id (links to address table)
billtoaddress_id (links to address table)
}
address {
city
country
state
name (company name?)
street1
street2
street3
zip
id
}
comlink {
isdeleted
type [Phone,Fax,E-mail,Cell,IM,FaceBook,Twitter,LinkedIn,Web Site,Other]
value
id
party_id (links to party table)
}
party {
isdeleted [t,f]
firstname
lastname
prefix
salutation
suffix
id
address_id
jobtitle_id
}
party_comlinks {
party_id (links to party table ex: fname lname...)
comlinks_id (links to comlink table phone, email, etc 1 item per row)
}
所以我想做的是用以下数据吸引所有客户:
customer.id,customer.accountid,customer.title,shipping.name,shipping.street1,shipping.street2,shipping.street3,shipping.city,shipping.state,shipping.zip,billing.name,billing.street1(etc) ,billing.city,billing.state,billing.zip,party.contactperson(as party.firstname party.lastname)并且有电话,电子邮件,传真
我不确定这是否可行,因为在系统中我认为您可以为每个客户提供多个送货信息,结算信息,联系信息...但是,如果每个送货信息和结算信息相同,我只需要一个一排电话,电子邮件,传真等,而不是每个电话,电子邮件,传真等多行 .
清楚如泥,对吗? :-)
edit -- may have gotten it now, but would still appreciate input
SELECT account.id, account.accountid, account.status, account.title AS "customer",
party.firstname AS "firstname", party.lastname as "lastname", address.name AS "billname",
address.street1 AS "billtostreet1", address.street2 AS "billtostreet2", address.city as "billtocity",
address.state AS "billtostate", address.zip AS "billtozip", address2.name AS "shiptoname",
address2.street1 as "shiptostreet1", address2.street2 AS "shiptostreet2", address2.city AS "shiptocity",
address2.state AS "shiptostate", address2.zip AS "shiptozip",
((SELECT a.value
FROM public.comlink a, party_comlinks b
WHERE b.party_id=party.id AND b.comlinks_id=a.id AND a.type='Phone')) AS "phone",
((SELECT a.value
FROM public.comlink a, party_comlinks b
WHERE b.party_id=party.id AND b.comlinks_id=a.id AND a.type='Fax')) AS "fax",
((SELECT a.value
FROM public.comlink a, party_comlinks b
WHERE b.party_id=party.id AND b.comlinks_id=a.id AND a.type='E-Mail')) AS "email"
FROM ( public.account account
INNER JOIN public.party party ON account.contact_id = party.id )
INNER JOIN public.comlink comlink ON party.id = comlink.party_id
INNER JOIN public.address address ON account.billtoaddress_id = address.id
INNER JOIN public.contact contact ON account.contact_id = contact.id
LEFT JOIN public.contact_shiptoaddress contact_shiptoaddress ON contact.id = contact_shiptoaddress.contact_id
LEFT JOIN public.address address2 ON contact_shiptoaddress.shiptoaddress_id = address2.id
WHERE account.isdeleted = 'f'
--WHERE ((comlink.type = 'E-Mail'))
--AND ((account.walkin is null OR (NOT ( account.walkin ))))
--AND ((NOT ( (account."status" = 'CustomerStatusInactive') )))
--AND ((account."prospect" is null
--OR (NOT ( account."prospect" ))))
ORDER BY account.id ASC
Actually, the INNER JOIN's were not what I needed... I needed to do LEFT JOIN's as described below.
1 回答
您必须通过多次明确加入联系人表来完成此操作 . 现在有一种方法可以根据数据设置一组“动态”输出列 .
例: