我想在 SQL 服务器中执行 group_concat

I want to do group_concat in SQL Server

我知道 group_concat 在 SQL Server 2008 中不起作用,但我想 group_concat

我的示例数据如下所示:

email address         | product code   
----------------------+---------------
garry123@gmail.com    | A123A  
garry123@gmail.com    | AB263    
ada121@hotmail.com    | 45632A   
ada121@hotmail.com    | 78YU
garry123@gmail.com    | 6543D 

我想要这个结果:

garry123@gmail.com | A123A,AB263,6543D 
ada121@hotmail.com | 45632A,78YU

我试过的代码:

SELECT
    c.EmailAddress,
    od.concat(productcode) as Product_SKU
FROM
    OrderDetails od
JOIN 
    Orders o ON od.OrderID = o.OrderID 
JOIN 
    Customers c ON c.CustomerID = o.CustomerID
WHERE
    o.OrderDate BETWEEN 01/01/2016 AND GETDATE()
GROUP BY 
    c.EmailAddress

我得到一个错误:

Cannot find either column "od" or the user-defined function or aggregate "od.concat", or the name is ambiguous.

但这不起作用。谁能告诉我正确的做法吗?

编辑后我正在尝试的代码:

SELECT
    c.EmailAddress,
    productcode = STUFF((SELECT ',' + od.productcode
                         FROM Orderdetails od
                         WHERE c.EmailAddress = od.EmailAddress
                         FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM
    OrderDetails od
JOIN 
    Orders o ON od.OrderID = o.OrderID 
JOIN 
    Customers c ON c.CustomerID = o.CustomerID
WHERE
    o.OrderDate BETWEEN 01/01/2016 AND GETDATE()

现在我收到这个错误:

Invalid column name 'EmailAddress'.

给你一个连接的例子(在本例中是自连接,但所有连接都有效)并为此使用 STUFF。注意 STUFF 中的 WHERE 子句。这就是 link 正确值的记录。

declare @test table
(
email varchar(50),
address varchar(50)
)

insert into @test VALUES
('garry123@gmail.com','A123A'),  
('garry123@gmail.com','AB263'),   
('ada121@hotmail.com','45632A'),   
('ada121@hotmail.com','78YU'),
('garry123@gmail.com','6543D')

SELECT DISTINCT 
       email,
       Stuff((SELECT ', ' + address  
              FROM   @test t2 
              WHERE  t2.email  = t1.email  
              FOR XML PATH('')), 1, 2, '') Address
FROM   @test t1  

编辑

好的,所以你想要的(你真正想要的)是:

declare @customers table
(
emailaddress varchar(50),
customerid int
)

insert into @customers VALUES
('garry123@gmail.com',1),  
('ada121@hotmail.com',2)   

declare @orders table
(
orderid int,
customerid int,
orderdate date
)

insert into @orders VALUES
(1, 1, '2017-06-02'),
(2, 1, '2017-06-05'),
(3, 1, '2017-07-13'),
(4, 2, '2017-06-13')

declare @orderdetails table
(
id int,
orderid int,
productcode varchar(10)
)

insert into @orderdetails VALUES
(1, 1, 'apple pie'),
(2, 1, 'bread'),
(3, 2, 'custard'),
(4, 2, 'orange'),
(5, 3, 'orange'),
(6, 4, 'orange')

SELECT DISTINCT c.EmailAddress, productcode=
STUFF((SELECT ',' + odc.productcode FROM 
(SELECT DISTINCT emailaddress,productcode FROM 
@orders o2 inner join @orderdetails od2 on
o2.orderid = od2.orderid
inner join @customers c2 ON c2.customerid = o2.customerid) odc 
WHERE odc.emailaddress=c.emailaddress 
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
FROM @OrderDetails od 
JOIN @Orders o ON od.OrderID = o.OrderID 
JOIN @Customers c ON c.CustomerID=o.CustomerID 
WHERE o.OrderDate BETWEEN '2016-01-01' AND getdate()

注意这里的变化。 STUFF 中的 SELECT 现在来自子查询,因此您可以按 EmailAddress 分组。

补充说明

您的目标是将按客户分组的产品代码串联起来(用电子邮件地址表示)。问题是产品代码在 orderdetails table 中,emailaddress 在客户 table 中,但是没有 link 这两者的字段。客户 table 与订单 table 具有一对多关系,订单 table 与订单详细信息 table 具有一对多关系。这是一个太多的抽象级别。因此,我们需要通过在产品代码和电子邮件地址之间提供直接 link 来帮助数据库。我们通过子查询来做到这一点。我希望这能让你更清楚。