一个简单的问题30分.

ghumorst 2002-06-19 11:24:30
select 语句中group by到底有什么作用??什么时候该用这个?能给我举个例子说明一下吗???
...全文
41 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cutepage 2002-06-20
  • 打赏
  • 举报
回复
什么叫做哪里弄来的啊?
你是英文的教材,他是中文的教材罢了。
luoyyh9 2002-06-20
  • 打赏
  • 举报
回复
特别是在前端程序里面做分类统计分析图的时候经常用到这种SQL语句
愉快的登山者 2002-06-20
  • 打赏
  • 举报
回复
做分类统计时,很有用,如:
select sum(amount), count(id) from tablename group by customid
QQRN 2002-06-19
  • 打赏
  • 举报
回复
晕倒……
GROUP BY Clause
Specifies the groups into which output rows are to be placed and, if aggregate functions are included in the SELECT clause <select list>, calculates a summary value for each group. When GROUP BY is specified, either each column in any non-aggregate expression in the select list should be included in the GROUP BY list, or the GROUP BY expression must match exactly the select list expression.



Note If the ORDER BY clause is not specified, groups returned using the GROUP BY clause are not in any particular order. It is recommended that you always use the ORDER BY clause to specify a particular ordering of the data.


Syntax
[ GROUP BY [ ALL ] group_by_expression [ ,...n ]
[ WITH { CUBE | ROLLUP } ]
]

不要问我,我是大英盲……
竟然我的只有英文版的,我哭……
楼上的,你的上哪里弄的????
QQRN 2002-06-19
  • 打赏
  • 举报
回复
晕倒……
GROUP BY Clause
Specifies the groups into which output rows are to be placed and, if aggregate functions are included in the SELECT clause <select list>, calculates a summary value for each group. When GROUP BY is specified, either each column in any non-aggregate expression in the select list should be included in the GROUP BY list, or the GROUP BY expression must match exactly the select list expression.



Note If the ORDER BY clause is not specified, groups returned using the GROUP BY clause are not in any particular order. It is recommended that you always use the ORDER BY clause to specify a particular ordering of the data.


Syntax
[ GROUP BY [ ALL ] group_by_expression [ ,...n ]
[ WITH { CUBE | ROLLUP } ]
]

不要问我,我是大英盲……
weixy 2002-06-19
  • 打赏
  • 举报
回复
GROUP BY 子句
指定用来放置输出行的组,并且如果 SELECT 子句 <select list> 中包含聚合函数,则计算每组的汇总值。指定 GROUP BY 时,选择列表中任一非聚合表达式内的所有列都应包含在 GROUP BY 列表中,或者 GROUP BY 表达式必须与选择列表表达式完全匹配。



说明 如果未指定 ORDER BY 子句,则使用 GROUP BY 子句不按任何特定的顺序返回组。建议始终使用 ORDER BY 子句指定具体的数据顺序。


语法
[ GROUP BY [ ALL ] group_by_expression [ ,...n ]
[ WITH { CUBE | ROLLUP } ]
]

参数
ALL

包含所有组和结果集,甚至包含那些任何行都不满足 WHERE 子句指定的搜索条件的组和结果集。如果指定了 ALL,将对组中不满足搜索条件的汇总列返回空值。不能用 CUBE 或 ROLLUP 运算符指定 ALL。

如果访问远程表的查询中有 WHERE 子句,则不支持 GROUP BY ALL 操作。

group_by_expression

是对其执行分组的表达式。group_by_expression 也称为分组列。group_by expression 可以是列或引用列的非聚合表达式。在选择列表内定义的列的别名不能用于指定分组列。



说明 text、ntext 和 image 类型的列不能用于 group_by_expression。


对于不包含 CUBE 或 ROLLUP 的 GROUP BY 子句,group_by_expression 的项数受查询所涉及的 GROUP BY 列的大小、聚合列和聚合值的限制。该限制从 8,060 字节的限制开始,对保存中间查询结果所需的中间级工作表有 8,060 字节的限制。如果指定了 CUBE 或 ROLLUP,则最多只能有 10 个分组表达式。

CUBE

指定在结果集内不仅包含由 GROUP BY 提供的正常行,还包含汇总行。在结果集内返回每个可能的组和子组组合的 GROUP BY 汇总行。GROUP BY 汇总行在结果中显示为 NULL,但可用来表示所有值。使用 GROUPING 函数确定结果集内的空值是否是 GROUP BY 汇总值。

结果集内的汇总行数取决于 GROUP BY 子句内包含的列数。GROUP BY 子句中的每个操作数(列)绑定在分组 NULL 下,并且分组适用于所有其它操作数(列)。由于 CUBE 返回每个可能的组和子组组合,因此不论指定分组列时所使用的是什么顺序,行数都相同。

ROLLUP

指定在结果集内不仅包含由 GROUP BY 提供的正常行,还包含汇总行。按层次结构顺序,从组内的最低级别到最高级别汇总组。组的层次结构取决于指定分组列时所使用的顺序。更改分组列的顺序会影响在结果集内生成的行数。



重要 使用 CUBE 或 ROLLUP 时,不支持区分聚合,如 AVG(DISTINCT column_name)、COUNT(DISTINCT column_name) 和 SUM(DISTINCT column_name)。如果使用这类聚合,SQL Server 将返回错误信息并取消查询。


©1988-2000 Microsoft Corporation。保留所有权利。USE pubs


SELECT type, AVG(price)
FROM titles
WHERE advance > $5000
GROUP BY type

下面是结果集:

type
------------ --------------------------
business 2.99
mod_cook 2.99
popular_comp 21.48
psychology 14.30
trad_cook 17.97

(5 row(s) affected)

只有预付款超过 $5,000 的行才包含在查询结果所显示的组中。


34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧