![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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