问题:
我正在尝试返回一个包含所有连接在一起的父项名称的列的联系人列表。请不要打扰告诉我这违反了第1范式,我已经知道了。我一直试图让这个工作很长时间,这可能是一个愚蠢的错误,但我现在需要一些帮助。
查询:
SELECT C.ID, C.SequenceNumber, C.ContactType, C.GUID, C.ContactCategory, MAP.Parents, C.LastName, C.FirstName, C.CompanyName, C.Title, C.Email, C.Phone, C.MobilePhone, C.Fax, C.Comments, C.StandardConfirmation, C.Active FROM
(
SELECT ContactTypeMapping.Contact_GUID, STUFF( ( SELECT ','+ [NAME]
FROM ContactParents a
WHERE a.GUID =b.GUID
FOR XML PATH('')),1 ,1, '') Parents
FROM ContactParents b, ContactTypeMapping
WHERE ContactTypeMapping.Parent_GUID=b.GUID
) MAP
INNER JOIN
(
SELECT Contact.ID, Contact.GUID, Contact.SequenceNumber,
Contact.ContactType, Contact.ContactCategory, Contact.LastName, Contact.FirstName,
Contact.CompanyName, Contact.Title, Contact.Email, Contact.Phone,
Contact.MobilePhone, Contact.Fax, Contact.Comments, Contact.StandardConfirmation, Contact.Active
FROM Contact
)C
ON (MAP.Contact_GUID=C.GUID)
目前的结果:
ID SequenceNumber ContactType GUID ContactCategory Parents LastName FirstName CompanyName Title Email Phone MobilePhone Fax Comments StandardConfirmation Active
15 4 2 95A566D0-DB83-4853-9CB7-E6CF3B1FF814 0 AParent Beard Kirk NULL Business Kirk_Beard@someplace.com 913-906-3333 NULL (913) 906-3434 NULL 0 1
15 4 2 95A566D0-DB83-4853-9CB7-E6CF3B1FF814 0 AnotherParent Beard Kirk NULL Business Kirk_Beard@someplace.com 913-906-3333 NULL (913) 906-3434 NULL 0 1
期望的结果:
15 4 2 95A566D0-DB83-4853-9CB7-E6CF3B1FF814 0 AParent,AnotherParent Beard Kirk NULL Business Kirk_Beard@someplace.com 913-906-3333 NULL (913) 906-3434 NULL 0 1
数据:
ContactParents表:
GUID NAME TYPE
C40A6F7E-F760-48D6-8BAF-E55EC7DC900D AParent Place
A651A0A3-5A50-45F1-AB4B-2B7FDCE9734C AnotherParent Place
ContactTypeMapping表:
Contact_GUID Parent_GUID ParentTable
95A566D0-DB83-4853-9CB7-E6CF3B1FF814 C40A6F7E-F760-48D6-8BAF-E55EC7DC900D Place
95A566D0-DB83-4853-9CB7-E6CF3B1FF814 A651A0A3-5A50-45F1-AB4B-2B7FDCE9734C Place
联系表:
ID GUID SequenceNumber ContactType LastName FirstName Title Email Phone MobilePhone Fax Comments Active ContactCategory CompanyName StandardConfirmation
15 95A566D0-DB83-4853-9CB7-E6CF3B1FF814 4 2 Beard Kirk Business Kirk_Beard@someplace.com 913-906-3333 NULL (913) 906-3434 NULL 1 0 NULL 0
答案 0 :(得分:5)
;WITH cp AS -- contact parents - initial join
(
SELECT cp.NAME, ctm.Parent_GUID
FROM ContactParents AS cp
INNER JOIN ContactTypeMapping AS ctm
ON cp.GUID = ctm.Contact_GUID
),
cm AS -- contact mapping with concatenated values
(
SELECT Parent_GUID, Parents = (
SELECT STUFF ((SELECT ','+ [NAME] FROM cp AS cp2
WHERE cp2.Parent_GUID = cp.Parent_GUID
FOR XML PATH('')),1 ,1, '')
)
FROM cp GROUP BY Parent_GUID
)
SELECT c.[GUID], cm.Parents --, other columns from c
FROM Contact AS c
INNER JOIN cm
ON c.[GUID] = cm.Parent_GUID;
为了演示我如何验证此查询返回正确的结果,这里是我在本地创建的表变量,我如何填充它们,以及引用表变量的稍微不同的查询:
DECLARE @ContactParents TABLE
(
[GUID] UNIQUEIDENTIFIER,
NAME VARCHAR(32),
[TYPE] VARCHAR(32)
);
INSERT @ContactParents VALUES
('C40A6F7E-F760-48D6-8BAF-E55EC7DC900D','AParent','Place'),
('A651A0A3-5A50-45F1-AB4B-2B7FDCE9734C','AnotherParent','Place');
DECLARE @ContactTypeMapping TABLE
(
Contact_GUID UNIQUEIDENTIFIER,
Parent_GUID UNIQUEIDENTIFIER,
ParentTable VARCHAR(32)
);
INSERT @ContactTypeMapping VALUES
('A651A0A3-5A50-45F1-AB4B-2B7FDCE9734C','95A566D0-DB83-4853-9CB7-E6CF3B1FF814','Place'),
('C40A6F7E-F760-48D6-8BAF-E55EC7DC900D','95A566D0-DB83-4853-9CB7-E6CF3B1FF814','Place');
DECLARE @Contact TABLE
(
ID INT,
[GUID] UNIQUEIDENTIFIER,
LastName VARCHAR(32),
FirstName VARCHAR(32)
--, other columns...
);
INSERT @Contact VALUES
(15, '95A566D0-DB83-4853-9CB7-E6CF3B1FF814', 'Beard', 'Kirk');
;WITH cp AS
(
SELECT cp.NAME, ctm.Parent_GUID
FROM @ContactParents AS cp
INNER JOIN @ContactTypeMapping AS ctm
ON cp.GUID = ctm.Contact_GUID
),
cm AS
(
SELECT Parent_GUID, Parents = (
SELECT STUFF ((SELECT ','+ [NAME] FROM cp AS cp2
WHERE cp2.Parent_GUID = cp.Parent_GUID
FOR XML PATH('')),1 ,1, '')
)
FROM cp
GROUP BY Parent_GUID
)
SELECT c.[GUID], cm.Parents --, other columns from c
FROM @Contact AS c
INNER JOIN cm
ON c.[GUID] = cm.Parent_GUID;
结果
GUID Parents
95A566D0-DB83-4853-9CB7-E6CF3B1FF814 AParent,AnotherParent