[英]SQL 2005: How to use GROUP BY with a sub query
以下非常簡單的查詢
select distinct guid, browser_agent
from tblMyGlossary
where browser_agent is not null
提供以下結果:
guid browser_agent
367DE2B8-88A5-4DA9-ACBB-C0864493DC1F Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
5DCB918E-DA56-4545-A4E3-D09B1B803422 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
998B8F37-2C9A-49EB-AA0B-CF88C4CC7BDF Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5
A0DD3BCB-E8A9-4434-A869-C343FB21F993 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)
我希望能夠計算唯一的browser_agent字符串的數量,所以我執行以下查詢:
select browser_agent, count(browser_agent) as 'count'
from
(
select distinct guid, browser_agent
from tblMyGlossary
where browser_agent is not null
)
group by browser_agent
order by 'count' desc;
問題是SQL 2005抱怨:
消息156,級別15,狀態1,行8關鍵字“組”附近的語法不正確。
任何人都可以解釋如何解決這個問題嗎? 我已經沒想完了。
非常感謝,
標記
您需要為派生表設置別名。
select browser_agent, count(browser_agent) as 'count'
from
(
select distinct guid, browser_agent
from tblMyGlossary
where browser_agent is not null
) a
group by browser_agent
order by 'count' desc;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.