我正在使用SQL Server Management Studio 2017,并且有一个SELECT
语句,我在子查询中使用STRING_AGG
函数,例如:
SELECT
[p].[test],
[p].[test],
....
(SELECT [Customers]
FROM
(SELECT
STRING_AGG([C].[Name] , ', ') AS [Customers]
FROM
[Project] AS [P]
RIGHT JOIN
[ProjectCustomer] AS [PC] ON [P].[ProjectKey] = [PC].[ProjectKey]
INNER JOIN
[Customer] AS [C] ON [PC].[CustomerKey] = [C].[CustomerKey]
GROUP BY
[P].[Name]) AS [t])
FROM
...
问题是当我执行它时,出现此异常:
子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
但是,如果我单独运行子查询,则选择它运行,并且它仅返回一个字符串值。有人可以解释一下我在做什么错吗?问候
答案 0 :(得分:0)
这种结构根本没有意义。我猜你想要:
select . . . ,
(select string_agg(c.name, ', ')
from ProjectCustomer pc join
customers c
on c.CustomerKey = pc.CustomerKey
where p.ProjectKey = pc.ProjectKey
) as customer_names
from p;
从手来说,这些表格对我来说再有用不过了。但这只是基于您的查询的猜测。