Write a SQL query to count the number of duplicate TRANSACTION_ID in an ORDERS DB2 table

SQLSQLiteDatabase

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
raja
Published on 01-Dec-2020 04:39:11
Advertisements