Home >Database >Mysql Tutorial >SQL query example describing COUNT function and GROUP BY
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.
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.
TRANSACTION_ID | ##ORDER_ID
|
A23118 | |
A45901 |
|
A67990 |
|
A23119 |
##IRN99781 |
p> A67921 | IRN56902 |
A23167 |
##COUNT(ORDER_ID) | IRN22345 |
##IRN56902 | |
IRN99781 | ##1|
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!