我的数据看起来像:
客户表
CustomerId CustomerName CustomerEmail
------------------------------------------
1 Ben Ben@gmail.com
2 Robert Robert@gmail.com
3 Paul Paul@gmail.com
客户联系人表
CustomerContactId CustomerId ContactName ContactEmail
----------------------------------------------------------
99 1 Lisa Lisa@msn.com
98 3 Jane Jane@msn.com
97 3 Wendy Wendy@msn.com
这是我正在寻找的结果:
[
{
"CustomerId": 1,
"Names": [ "Ben","Lisa" ],
"Emails": [ "Ben@gmail.com","Lisa@msn.com" ]
},
{
"CustomerId": 2,
"Names": [ "Robert" ],
"Emails": [ "Robert@gmail.com" ]
},
{
"CustomerId": 3,
"Names": [ "Paul","Jane","Wendy" ],
"Emails": [ "Paul@gmail.com","Jane@msn.com","Wendy@msn.com" ]
}
]
我尝试过的: 我很尴尬地说我还没有接近:
SELECT
Customers.CustomerId,
STUFF( ISNULL(',' + Customers.CustomerName, '') + ISNULL(',' + CustomerContacts.ContactName, ''),1,1,'') as Names
FROM Customers
FULL JOIN CustomerContacts
ON Customers.CustomerId = CustomerContacts.CustomerId
GROUP BY Customers.CustomerId;