在嵌套SELECT中比较Count from Outer SELECT?

时间:2012-11-26 08:58:36

标签: sql sql-server sql-server-2008

我有这个SQL查询,

select * from (select * from .......) as a
where 1 = case when CountOfInnerSelect = 1 Then 1 ELSE ............

我可以在外部SELECT中获得内部选择的计数吗?

2 个答案:

答案 0 :(得分:1)

是的:

select * from (select count(0) as cnt from .......) as a
where 1 = case when a.cnt = 1 Then 1 ELSE ............

答案 1 :(得分:1)

您的WHERE Clause没有多大意义,因为它像过滤器一样应用于此处。 (即;类似于WHERE myCount = 1

SELECT * FROM
(SELECT c1,c2,c3,..,Cn,COUNT(*) AS myCount
 FROM YourTable
 GROUP BY c1,c2,c3,..,Cn
) A
WHERE 1 = CASE myCount WHEN 1 THEN 1 ELSE... END