Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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 选择多列子查询中x的位置_Sql_Sql Server - Fatal编程技术网

Sql 选择多列子查询中x的位置

Sql 选择多列子查询中x的位置,sql,sql-server,Sql,Sql Server,如果要选择结果在子查询的特定列中的位置,如何指定该子查询中的检查起始列 Select x from foo where x in (Select y, Max(z) as MaxEntry From bar Group By y) 假设我从中选择: Y Z 1 2 1 4 2 7 2 8 我想看看x是否在4

如果要选择结果在子查询的特定列中的位置,如何指定该子查询中的检查起始列

Select x from foo 
where x  in (Select y, Max(z) as MaxEntry
From bar 
Group By y)
假设我从中选择:

   Y                Z
   1                2
   1                4
   2                7
   2                8 

我想看看x是否在4或8组数据中

若你们只检查x列,那个么你们可以这样写

Select x from foo 
where x  in (Select x from bar where blah)
Select x from foo 
where exists(Select * from bar where bar.x = foo.x and bar.y = foo.y )
但是如果你想检查x和y,你可以这样写

Select x from foo 
where x  in (Select x from bar where blah)
Select x from foo 
where exists(Select * from bar where bar.x = foo.x and bar.y = foo.y )
对于您的查询,您不需要在select处写入y

Select x from foo 
where x  in (Select Max(z) as MaxEntry
From bar 
Group By y)
我想你想要:

SELECT x
FROM foo
WHERE EXISTS
(SELECT 1
 FROM bar
 GROUP BY y
 HAVING MAX(bar.z) = foo.x)

如果我正确理解了你的问题,那么这应该是可行的:

Select x from foo 
join (Select y, Max(z) as MaxEntry
     From bar 
     Group By y) 
as m on m.MaxEntry = x

在子查询中选择
y
没有意义。只需选择
x
。如果您需要更多,请在问题中更加具体。更新以显示您为什么关心y或max(z)的更多细节?您的内部查询有多复杂并不重要。你到底想选什么?您可能需要提供一些示例数据和所需结果。您不需要指定
y
,然后在
中选择
。编辑坚持如果你要按它分组,你需要它吗?