gpt4 book ai didi

sql-server - 子查询返回超过 1 个值。子查询时不允许这样做

转载 作者:行者123 更新时间:2023-12-04 16:06:39 25 4
gpt4 key购买 nike

我正在尝试使用 SQL 从 SCCM 返回 Windows 更新合规性数据,我已从内置 SSRS 报告中提取查询。

我没有为每个@colname(计算机组)运行单独的查询,而是尝试通过将@colname 声明为一个表并插入多个值来组合为一个查询。

SQL返回的错误是:

Msg 512, Level 16, State 1, Line 6
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

我的代码:

DECLARE @title VARCHAR(500);
DECLARE @colname TABLE (name VARCHAR(100));

SET @title = 'ADR | Workstation Software Updates 2017-12-14 09:01:38';

INSERT INTO @colname
VALUES ('All Alpha Workstations'), ('All Beta Workstations'), ('All Delta Workstations');

SELECT DISTINCT
COUNT(*) [Total Clients], li.title, coll.name,
SUM(CASE WHEN ucs.status = 3 OR ucs.status = 1 THEN 1 ELSE 0 END) AS 'Installed / Not Applicable',
SUM(CASE WHEN ucs.status = 2 THEN 1 ELSE 0 END) AS 'Required',
SUM(CASE WHEN ucs.status = 0 THEN 1 ELSE 0 END) as 'Unknown',
ROUND((CAST(SUM(CASE WHEN ucs.status = 3 OR ucs.status = 1 THEN 1 ELSE 0 END) AS float) / COUNT(*)) * 100, 2) AS 'Success %',
ROUND((CAST(COUNT(CASE WHEN ucs.status NOT IN ('3', '1') THEN '*' END) AS FLOAT) / COUNT(*)) * 100, 2) AS 'Not Success%'
FROM
v_Update_ComplianceStatusAll UCS
INNER JOIN
v_r_system sys ON ucs.resourceid = sys.resourceid
INNER JOIN
v_FullCollectionMembership fcm ON ucs.resourceid = fcm.resourceid
INNER JOIN
v_collection coll ON coll.collectionid = fcm.collectionid
INNER JOIN
v_AuthListInfo LI ON ucs.ci_id = li.ci_id
WHERE
li.title = @title
AND coll.name = (SELECT name FROM @colname) --and ucs.status=2
GROUP BY
li.title, coll.name
ORDER BY
1

任何帮助表示赞赏。谢谢

最佳答案

子查询 select name from @collname 返回多个值,这就是你不能使用 = 的原因。

使用 IN 谓词代替 =:

where li.title=@title and coll.name in (select name from @collname)

关于sql-server - 子查询返回超过 1 个值。子查询时不允许这样做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47938931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com