• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

sql group by 语句用法

mysql 搞代码 4年前 (2022-01-09) 16次浏览 已收录 0个评论

合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句。
GROUP BY 语句
GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。

SQL GROUP BY 语法
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
SQL GROUP BY 实例
我们拥有下面这个 “Orders” 表:

O_Id OrderDate OrderPrice Customer
1 2008/12/29 1000 Bush
2 2008/11/23 1600 Carter
3 2008/10/05 700 Bush
4 2008/09/28 300 Bush
5 2008/08/06 2000 Adams
6 2008/07/21 100 Carter

现在,我们希望查找每个客户的总金额(总订单)。

我们想要使用 GROUP BY 语句对客户进行组合。

我们使用下列 SQL 语句:

SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
结果集类似这样:

Customer SUM(OrderPrice)
Bush 2000
Carter 1700
Adams 2000

很棒吧,对不对?

让我们看一下如果省略 GROUP BY 会出现什么情况:

SELECT Customer,SUM(OrderPrice) FROM Orders结果集类似这样:

Customer SUM(OrderPrice)
Bush 5700
Carter 5700
Bush 5700
Bush 5700
Adams 5700
Carter 5700

上面的结果集不是我们需要的。

那么为什么不能使用上面这条 SELECT 语句呢?解释如下:上面的 SELECT 语句指定了两列(Customer 和 SUM(OrderPrice))。”SUM(OrderPrice)” 返回一个单独的值(”OrderPrice” 列的总计),而 “Customer” 返回 6 个值(每个值对应 “Orders” 表中的每一行)。因此,我们得不到正确的结果。不过,您已经看到了,GROUP BY 语句解决了这个问题。
GROUP BY 一个以上的列
我们也可以对一个以上的列应用 GROUP BY 语句,就像这样:

SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders
GROUP BY Customer,OrderDate

综合实例

> create table employee(
2> ID int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> GO
1>
2> insert into employee (ID, name, salary, start_date, city, region)
3> values (1, ‘Jason’, 40420, ’02/01/94′, ‘New York’, ‘W’)
4> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (2, ‘Robert’,14420, ’01/02/95′, ‘Vancouver’,’N’)
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (3, ‘Celia’, 24020, ’12/03/96′, ‘Toronto’, ‘W’)
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (4, ‘Linda’, 40620, ’11/04/97′, ‘New York’, ‘N’)
3> GO

(1 rows affected)
1> insert into来2源gaodaima#com搞(代@码&网 employee (ID, name, salary, start_date, city, region)
2> values (5, ‘David’, 80026, ’10/05/98′, ‘Vancouver’,’W’)
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (6, ‘James’, 70060, ’09/06/99′, ‘Toronto’, ‘N’)
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (7, ‘Alison’,90620, ’08/07/00′, ‘New York’, ‘W’)
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (8, ‘Chris’, 26020, ’07/08/01′, ‘Vancouver’,’N’)
3> GO

(1 rows affected)
1> insert into employee (ID, name, salary, start_date, city, region)
2> values (9, ‘Mary’, 60020, ’06/09/02′, ‘Toronto’, ‘W’)
3> GO

(1 rows affected)
1>
2> * from employee
3> GO
ID name salary start_date city region
———– ———- ———– ———————– ———- ——
1 Jason 40420 1994-02-01 00:00:00.000 New York W
2 Robert 14420 1995-01-02 00:00:00.000 Vancouver N
3 Celia 24020 1996-12-03 00:00:00.000 Toronto W
4 Linda 40620 1997-11-04 00:00:00.000 New York N
5 David 80026 1998-10-05 00:00:00.000 Vancouver W
6 James 70060 1999-09-06 00:00:00.000 Toronto N
7 Alison 90620 2000-08-07 00:00:00.000 New York W
8 Chris 26020 2001-07-08 00:00:00.000 Vancouver N
9 Mary 60020 2002-06-09 00:00:00.000 Toronto W

(9 rows affected)
1>
2> –GROUP BY clause with an aggregator ‘SUM()’.
3>
4> SELECT region, SUM(Salary)
5> FROM Employee
6> WHERE ID BETWEEN 1 AND 10
7> GROUP BY Region
8> GO
region
—— ———–
N 151120
W 295106

(2 rows affected)
1>
2>
3> drop table employee
4> GO
1>


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:sql group by 语句用法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址