What is the method for performing a group query in DB2?

In DB2, the method for grouping queries is to use the GROUP BY clause. In the SELECT statement, you can specify the columns to group by by adding the GROUP BY clause after the column name. For example:

SELECT 列1, 列2, ... FROM 表名
GROUP BY 列1, 列2, ...

In the GROUP BY clause, you can specify one or more columns as the criteria for grouping. The results of the query will be grouped according to the specified columns, and the results of each group will be merged into one row.

In addition to grouping columns, aggregate functions (such as SUM, COUNT, AVG, etc.) can also be used in the SELECT statement to calculate and analyze data after grouping.

For example, the following query retrieves data from an order table, grouping orders by customer ID and calculating the total order amount for each customer.

SELECT customer_id, SUM(order_amount) AS total_amount
FROM orders
GROUP BY customer_id

The search will provide the customer ID and total order amount for each customer.

bannerAds