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

SQL query example describing COUNT function and GROUP BY

王林
Release: 2023-09-05 09:01:10
forward
1450 people have browsed it

描述 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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template