Home >Database >Mysql Tutorial >SQL query example describing COUNT function and GROUP BY

SQL query example describing COUNT function and GROUP BY

王林
王林forward
2023-09-05 09:01:101418browse

描述 COUNT 函数和 GROUP BY 的 SQL 查询示例

Question: Write a query on the TRANSACTIONS DB2 table that lists the number of orders (ORDER_ID) assigned to a specific transaction (TRANSACTION_ID).

Solution

We can use the following query to find the order quantity assigned to a specific transaction ID on the TRANSACTIONS DB2 table.

Example

SELECT TRANSACTION_ID, COUNT(ORDER_ID) FROM TRANSACTIONS
   GROUP BY TRANSACTION_ID

We will use GROUP BY function on ORDER_ID to get the results in order. The COUNT function will count the order quantity. For example, we have the following DB2 ORDERS table.

IRN22345A23118##IRN22345IRN22345 IRN56902##IRN99781IRN56902## Results Our DB2 query will return the following results.
TRANSACTION_ID

##ORDER_ID

A45901

A67990

A23119

p> A67921

A23167

TRANSACTION_ID32##1
##COUNT(ORDER_ID)

IRN22345

##IRN56902

IRN99781

The above is the detailed content of SQL query example describing COUNT function and GROUP BY. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete