sql group by 语句用法

SQL GROUP BY 语法SELECT column_name,
aggregate_function(column_name)FROM table_nameWHERE column_name
operator valueGROUP BY column_nameSQL GROUP BY 实例大家具有下边这几个Orders 表:

SELECT Customer,OrderDate,SUM(OrderPrice) FROM OrdersGROUP BY
Customer,OrderDate

那么为何不能够应用方面那条 SELECT 语句呢?解释如下:下面的 SELECT
语句钦命了两列。SUM(OrderPrice卡塔尔国 重回一个独立的值,而 Customer 再次回到 6
个值。由此,大家得不到正确的结果。然而,您已经观察了,GROUP BY
语句消除了那个主题素材。GROUP BY 二个之上的列我们也得以对二个之上的列应用
GROUP BY 语句,就如这么:

情商函数 (比方 SUM卡塔尔(قطر‎ 平常供给增多 GROUP BY 语句。GROUP BY 语句GROUP BY
语句用于结合合计函数,依照三个或八个列对结果集举行分组。

总的来讲实例

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

前几天,我们期望物色种种顾客的总金额。

SELECT Customer,SUM(OrderPrice卡塔尔国 FROM OrdersGROUP BY
Customer结果集相仿那样:

很棒吧,对不对?

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 GO12
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 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)12 select * from employee3 GOID 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)12 –GROUP BY clause with an aggregator ‘SUM()’.34
SELECT region, SUM(Salary)5 FROM Employee6 WHERE ID BETWEEN 1 AND 107
GROUP BY Region8 GOregion—— ———–N 151120W 295106

(2 rows affected)123 drop table employee4 GO1

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

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

Customer SUM(OrderPrice) Bush 2000 Carter 1700 Adams 2000

SELECT Customer,SUM(Order普赖斯卡塔尔(قطر‎ FROM Orders结果集相仿那样:

小编们使用下列 SQL 语句:

让大家看一下万一省略 GROUP BY 会现身什么情形:

上面包车型大巴结果集不是大家须求的。

相关文章