简体   繁体   English

SQL 2005:如何将GROUP BY与子查询一起使用

[英]SQL 2005: How to use GROUP BY with a sub query

The following very simple query 以下非常简单的查询

select distinct guid, browser_agent
from tblMyGlossary
where browser_agent is not null

provides the following results: 提供以下结果:

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)

Ii want to be able to count the number of unique browser_agent strings, so I am performing the following query: 我希望能够计算唯一的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;

Problem is SQL 2005 is complaining: 问题是SQL 2005抱怨:

Msg 156, Level 15, State 1, Line 8 Incorrect syntax near the keyword 'group'. 消息156,级别15,状态1,行8关键字“组”附近的语法不正确。

Can anyone shed any light on how to resolve this please? 任何人都可以解释如何解决这个问题吗? I've run out of ideas. 我已经没想完了。

Many thanks, 非常感谢,

Mark 标记

You need to alias your derived table. 您需要为派生表设置别名。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM