- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 |
- Related Articles
- Write a DB2 query to find out all the duplicate INVOICE_ID in ORDERS DB2 table?
- Write the DB2 SQL query to find the third highest ORDER_TOTAL in a ORDERS DB2 table
- Write the syntax to declare a scrollable cursor on the ORDERS DB2 table.
- MySQL query to count number of duplicate values in a table column
- How to update DB2 table with a duplicate primary key?
- How will you create a new TRIGGER on the ORDERS DB2 table? Give the syntax of TRIGGER
- How to count the number of duplicate rows in an R data frame?
- Implementation of a table level locks in a COBOL-DB2 program during SQL execution
- How can I write an SQL IN query with a Python tuple?
- How to get number of rows in a table without using count(*) MySQL query?
- Create table SQL query in SAP HANA
- Count the occurrences of specific records (duplicate) in one MySQL query
- MySQL query to find duplicate tuples and display the count?
- How to count SQL table columns using Python?
- Count of matrices (of different orders) with given number of elements in C++

Advertisements