我正在为客户创建报告。在其中,我需要合并来自4个表的信息。直到最后一张表(我只使用一列),一切都很好。此列包含电话和电子邮件(全部合而为一)。我需要在单独的列中显示电子邮件地址和电话号码,但是它们来自同一列。
我可以同时显示两者,但它们分别显示在不同的行中。我还可以复制该列,然后在excel中对其进行排序,但是客户需要能够自己完成此操作,而后无需对其进行编辑。
SELECT CustomerProcessO.GID,
CustomerProcessO.FOIF,
Customers.FirstName,
Customers.LastName,
SalesPersons.FirstName,
SalesPersons.LastName,
DynamicListData.DataContent AS Email,
DynamicListData.DataContent AS Phone
FROM CustomerProcessO
LEFT OUTER JOIN Customers ON CustomerProcessO.id = Customers.id
LEFT OUTER JOIN SalesPersons ON CustomerProcessO.GIDSalesPerson = SalesPersons.GID
LEFT OUTER JOIN DynamicListData ON ParentId = Customers.id
WHERE DynamicListData.KeyField = 'EMAILS'
OR DynamicListData.KeyField = 'PHONES'
我得到的结果是正确的,但是我在两行中分别得到了电话号码和电子邮件。我需要一栏包含电子邮件,一栏包含电话号码
答案 0 :(得分:2)
您可以两次连接同一张表:
SELECT dld1.DataContent AS Email, dld2.DataContent AS Phone
...
LEFT JOIN DynamicListData dld1 ON dld1.ParentId = Customers.id AND dld1.KeyField = 'EMAILS'
LEFT JOIN DynamicListData dld2 ON dld2.ParentId = Customers.id AND dld2.KeyField = 'PHONES'
答案 1 :(得分:0)
SELECT CustomerProcessO.GID, CustomerProcessO.FOIF, Customers.FirstName, Customers.LastName, SalesPersons.FirstName, SalesPersons.LastName,
DLD1.Phone, DLD2.Email
FROM CustomerProcessO
LEFT OUTER JOIN Customers ON CustomerProcessO.id = Customers.id
LEFT OUTER JOIN SalesPersons ON CustomerProcessO.GIDSalesPerson = SalesPersons.GID
LEFT OUTER JOIN (SELECT ParentId, KeyField, DataContent AS [Phone] FROM DynamicListDate) DLD1 ON DLD1.ParentId = Customers.id AND DLD1.KeyField = 'PHONES'
LEFT OUTER JOIN (SELECT ParentId, KeyField, DataContent AS [Email] FROM DynamicListDate) DLD2 ON DLD2.ParentId = Customers.id AND DLD2.KeyField = 'EMAILS'