2013-08-24 60 views
0

我在SQL Server中的以下相关子查询,工作正常如何从相关子查询where子句添加到列

select *, [Status]=(select Max([Status]) from Data_121EmailLog o2 
where o2.Data_121Id = o1.Data_121Id) from Data_121 o1 

你可以看到发生了什么,从这张截图怎么回事

enter image description here

然而,当我尝试添加,其中在生成的子查询中列第不起作用

enter image description here

如何在[Status]列中添加where子句。在我的例子,应返回1分的结果只有1个纪录的2

+0

你不能在'哪里使用列别名'子句:http://stackoverflow.com/a/6591216/1163867 – MarcinJuraszek

回答

0

继MarcinJuraszek的评论链接状态解决了这个问题对我来说

SELECT * FROM (
select *, [Status]=(select Max([Status]) from Data_121EmailLog o2 
where o2.Data_121Id = o1.Data_121Id) from Data_121 o1) a where a.[Status] = 2