您好,欢迎访问一九零五行业门户网

sql group by 语句用法

合计函数 (比如 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 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>
其它类似信息

推荐信息