2012-11-11 86 views
0

我有這樣的查詢與子查詢as clicks 我想做的事情,然後where clicks > 0,但我得到一個錯誤,這不是一個公認的列。使用父母嵌套查詢結果WHERE子句

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

可能重複(http://stackoverflow.com/questions/8370114/referring-to-一列別名在-A-where子句) – Pondlife

回答

1

雖然我與評論,這是一個可能重複的同意,我也想指出,相關子查詢可能是低效的;二鳥一石,你可以這樣做:的[參考列別名WHERE子句]

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