在parent where子句中使用嵌套查询结果

时间:2012-11-11 23:02:31

标签: tsql

我有一个子查询as clicks的查询 我想做where clicks > 0,但是我收到一个错误,这不是一个可识别的列。

SELECT top 10 *, (select count(*) FROM po_events aeclk WITH(NOLOCK) where (aeclk.[source] = ae.[source]) as clicks

FROM archived_events ae with(NOLOCK) 
where clicks > 0

1 个答案:

答案 0 :(得分:1)

虽然我同意这可能是重复的评论,但我还想指出相关的子查询可能效率低下;一石二鸟,你可以这样做:

SELECT TOP 10
        ae.*
      , aeclk.clicks
FROM    archived_events ae
        JOIN ( SELECT   source
                      , COUNT(*) AS clicks
               FROM     po_events
               GROUP BY source
             ) aeclk ON aeclk.[source] = ae.[source]
WHERE   clicks > 0