DEV Community

wanglei
wanglei

Posted on

GROUP BY Clause

The GROUP BY clause is used together with the SELECT statement to group the same data. You can group one or more columns, but the columns to be grouped must exist.

Syntax

SELECT 
{ * | [column, ...] }
[ FROM from_item [, ...] ]
[ WHERE condition ]
[ GROUP BY grouping_element [, ...] ]
[ ORDER BY {expression [ ASC | DESC ] }];
Enter fullscreen mode Exit fullscreen mode

Parameter Description
GROUP BY clause

Groups the query results based on the values of one or more columns. The query results with the same value are in the same group.

The GROUP BY clause is placed after the WHRER clause and before the ORDER BY clause in the SELECT statement.

Examples
The customer_t1 table contains duplicate values of c_first_name. The duplicate values of c_first_name are in the same group, and the sum of the values of Amount is calculated.

openGauss=# SELECT c_first_name, sum(Amount)  FROM customer_t1 GROUP BY c_first_name;
 c_first_name | sum
--------------+------
 James        | 5000
 Grace        | 1000
 Local        | 3000
              |
 Joes         | 2200
 Lily         | 3000
(6 rows) 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)