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

Updated on: 01-Dec-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements