Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Write a SQL query to count the number of duplicate TRANSACTION_ID in an ORDERS DB2 table
We can find the duplicate TRANSACTION_ID in the ORDERS DB2 table using the below query:
Example
SELECT TRANSACTION_ID, COUNT(*) AS TRANSACTION_COUNT FROM ORDER GROUP BY TRANSACTION_ID HAVING COUNT(*) > 1
The purpose of COUNT(*) is to count the number of rows. We will group the result based on the TRANSACTION_ID using GROUP BY function and to display the duplicate transaction ids, we will place a predicate using HAVING statement for COUNT(*) greater than one.
For example, consider the below TRANSACTIONS DB2 table:
|
TRANSACTION_ID |
TRANSACTION_STATUS |
| IRN22345 |
PAID |
| IRN22345 |
PAID |
| IRN22345 |
PAID |
| IRN56902 |
PAID |
| IRN99781 |
UNPAID |
| IRN56902 |
PAID |
The query will give the below result:
|
TRANSACTION_ID |
TRANSACTION_COUNT |
| IRN22345 |
3 |
| IRN56902 |
2 |
| IRN99781 |
1 |
Advertisements
