The group by statement is used in combination with the aggregate function to group result sets based on one or more columns.

Source: Internet
Author: User

1 drop table orders; 2 create table orders ( 3 o_id int auto_increment primary key, 4 orderdate date, 5 orderprice int, 6 customer varchar(100) 7 ); 8 9 insert into orders(orderdate, orderprice, customer) values(‘2008-12-29‘, 1000, ‘Bush‘);10 insert into orders(orderdate, orderprice, customer) values(‘2008-11-23‘, 1600, ‘Carter‘);11 insert into orders(orderdate, orderprice, customer) values(‘2008-10-05‘, 700, ‘Bush‘);12 insert into orders(orderdate, orderprice, customer) values(‘2008-09-28‘, 300, ‘Bush‘);13 insert into orders(orderdate, orderprice, customer) values(‘2008-08-06‘, 2000, ‘Adams‘);14 insert into orders(orderdate, orderprice, customer) values(‘2008-07-21‘, 100, ‘Carter‘);15 16 select * from orders;17 18 SELECT customer,SUM(orderprice) FROM orders19 GROUP BY customer;

We have the following "orders" table:

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

 

We use the following SQL statement:

SELECT Customer,SUM(OrderPrice) FROM OrdersGROUP BY Customer

The result set is similar to the following:

Customer Sum (orderprice)
Bush 2000
Carter 1700
Adams 2000

The group by statement is used in combination with the aggregate function to group result sets based on one or more columns.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.