Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 子查询返回的值超过1时出错_Sql_Sql Server - Fatal编程技术网

Sql 子查询返回的值超过1时出错

Sql 子查询返回的值超过1时出错,sql,sql-server,Sql,Sql Server,下面是我试图执行的查询,结果出现了错误。这是最初的错误 Msg 512,级别16,状态1,第36行子查询返回的值超过1。当子查询在=、!=、=或者当子查询用作表达式时 作为user1@abc.com我想查看所有直接向我汇报的记录,以及那些直接向我下属汇报的记录。根据这些数据,我想看到EmployeeID 273,16274285286275276276,23 获取>=要访问的所有记录的目标 user1@abc.com数量下面是表中的当前数据 大家好,我已经弄明白了,下面是运行的代码 类似=、!=

下面是我试图执行的查询,结果出现了错误。这是最初的错误

Msg 512,级别16,状态1,第36行子查询返回的值超过1。当子查询在=、!=、=或者当子查询用作表达式时

作为user1@abc.com我想查看所有直接向我汇报的记录,以及那些直接向我下属汇报的记录。根据这些数据,我想看到EmployeeID 273,16274285286275276276,23

获取>=要访问的所有记录的目标 user1@abc.com数量下面是表中的当前数据

大家好,我已经弄明白了,下面是运行的代码

类似=、!=、=只需要比较1个值。 因此,具有多条记录的结果将导致错误

但我们可以使用EXISTS来实现这一点

...
WHERE A.ManagerEmail = 'user1@abc.com'
AND EXISTS
(
  SELECT 1
  FROM TOrganization_Hierarchy B
  WHERE B.ManagerEmail = A.ManagerEmail
    AND B.Level <= A.Level
)
顺便说一句,从数据上看,与同一个ManagerID匹配可能更有效

...
WHERE A.ManagerEmail = 'user1@abc.com'
AND EXISTS
(
  SELECT 1
  FROM TOrganization_Hierarchy B
  WHERE B.ManagerID = A.ManagerID
    AND B.Level <= A.Level
)

我很确定你想要经理的级别。这需要正确的关联子句:

select oh.*
from TOrganization_Hierarchy oh
where oh.ManagerEmail = 'user1@abc.com' and
      oh.Level >= (select oh2.Level
                   from TOrganization_Hierarchy oh2
                   where oh2.Email = oh.ManagerEmail
                  ) ;
请注意,我还更改了表别名,因此它们是有意义的

请注意,如果要为给定的管理器设置最高级别,可以使用max:


这里的错误非常明确。您不理解的错误是什么,我们可以尝试详细说明。向我们展示一些示例表数据和预期结果。作为格式化文本,而不是图像。@jarlh,我已经用预期结果修改了我的原始问题。谢谢@Gordon Linoff。也许我的问题不清楚,但是user1@abc.com我想查看所有直接向我汇报的记录,以及那些直接向我下属汇报的记录。根据这些数据,我想看到EmployeeID 273,16274285286275276276,23。我希望我的问题现在清楚多了。
...
WHERE A.ManagerEmail = 'user1@abc.com'
AND EXISTS
(
  SELECT 1
  FROM TOrganization_Hierarchy B
  WHERE B.ManagerID = A.ManagerID
    AND B.Level <= A.Level
)
select oh.*
from TOrganization_Hierarchy oh
where oh.ManagerEmail = 'user1@abc.com' and
      oh.Level >= (select oh2.Level
                   from TOrganization_Hierarchy oh2
                   where oh2.Email = oh.ManagerEmail
                  ) ;
select oh.*
from TOrganization_Hierarchy oh
where oh.ManagerEmail = 'user1@abc.com' and
      oh.Level = (select max(oh2.Level)
                  from TOrganization_Hierarchy oh2
                  where oh2.ManagerEmailEmail = oh.ManagerEmail
                 ) ;