Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Dupes involving a compound key

## Suppose that there are two tables: TXN and TXN_DEAL. The latter is a child or sub-type table. TXN has five composite keys, which are repeated in the child table. I am trying to find multiple occurrences in child. In other words, I want to find TXN records that are associated to multiple TXN_DEAL records and the number of TXN_DEAL records associated to them. How do I do that in SQL?

Suppose that there are two tables: TXN and TXN_DEAL. The latter is a child or sub-type table. TXN has composite keys -- five of them to be exact, which, of course, are repeated in the child table. I am trying to find multiple occurrences in child. In other words, I want to find TXN records that are associated to multiple TXN_DEAL records and the number (count) of TXN_DEAL records associated to them. How do I do that in SQL?

So, something like this:

```TXN
PK col1
PK col2
PK col3
PK col4
PK col5
datacol6
datacol7

TXN_DEAL
PK col1 FK
PK col2 FK
PK col3 FK
PK col4 FK
PK col5 FK
PK somecolumn
datacol8
datacol9```

Notice how the child table has five foreign key columns, the same ones as in the TXN table, but an additional sixth column in the primary key. That sixth column is necessary because if there were only five columns in the primary key for TXN_DEAL, there would only be one row per key possible. You said you're looking for duplicates, which means that the five columns alone are not the primary key.

What we want to do now is simply count TXN_DEAL rows which are duplicated on the five FKs alone. This can be achieved with COUNT and GROUP BY, with HAVING to filter out the singletons. Then we simply join the TXN table to the dupes query as a derived table.

```select TXN.col1
, TXN.col2
, TXN.col3
, TXN.col4
, TXN.col5
, TXN.datacol6
, TXN.datacol7
, TXN_DEAL_dupes.rows  as TXN_DEAL_rows
from TXN
inner
join (
select col1
, col2
, col3
, col4
, col5
, count(*) as rows
from TXN_DEAL
group
by col1
, col2
, col3
, col4
, col5
having count(*) > 1
) as TXN_DEAL_dupes
on TXN.col1 = TXN_DEAL_dupes.col1
and TXN.col2 = TXN_DEAL_dupes.col2
and TXN.col3 = TXN_DEAL_dupes.col3
and TXN.col4 = TXN_DEAL_dupes.col4
and TXN.col5 = TXN_DEAL_dupes.col5```

Joining to a derived table is almost always the simplest method when compound keys are involved, and is essential when a count is required at the same time.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### EnterpriseDB looks to grow market for PostgreSQL

Enterprises are increasingly using the open source PostgreSQL database. Read in this Q&A where the growth is coming from and how ...

• ### 3 of the top use cases for graph databases

Graph databases establish many unique relationships between data points. These unusual relationships are beneficial in many use ...

• ### Dell EMC Streaming Data Platform integrates open source technology

Dell combines several open source streaming data technologies, including Apache Kafka, Apache Flink and Pravega, to create a new ...

• ### What Salesforce means for Tableau in the cloud

After the Salesforce acquisition, users have wondered about the future of Tableau in the cloud. Experts weigh in on how the ...

• ### MicroStrategy analytics suite fosters insurance co-op's growth

Using MicroStrategy's BI platform, a Canada-based insurance co-operative has been able to spread analytics to end users ...

• ### Yellowbrick Data, MicroStrategy partnership aims to enhance analytics

The new partnership will enable flexible scalability, support for mixed workloads and multi-cloud support. The goal is to provide...

## SearchSAP

• ### 5 tips for a successful S/4HANA Cloud implementation

Moving to S/4HANA public cloud can help you save on IT maintenance and simplify real-time analysis. Here's a look at what you can...

• ### Take care of data before SAP S/4HANA migration

In this Q&A, Rajesh Rengarethinam of ERP security vendor Appsian discusses why reviewing data security and business processes are...

Business Suite 7 will receive mainstream maintenance until 2027, a decision industry observers say reflects the difficulty of an ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Why the 8 Java primitive data types are not objects

What's the difference between Java primitive types and objects? To start, don't classify primitive types as objects. Let's ...

• ### A brief history of Java: How it forever changed programming

The Java programming language is an important tool in an application developer's quiver. But Java has also undergone many changes...

• ### How to properly perform Java String comparisons

Don't let incorrect Java String object comparisons bog down your code. Follow this tutorial to understand the differences between...

## SearchDataCenter

• ### Learn the main Linux OS components

Linux is one operating choice to run on your infrastructure. Get started with these terms to discover how the OS works and how it...

• ### New Dell EMC PowerEdge servers are built for the rugged edge

New Dell Streaming Media Platform includes two small form factor PowerEdge servers and a Modular Data Center. Dell aims the ...

• ### Initiative aims to improve data center incident reporting

The Data Center Incident Reporting Network hopes to pull back the smoke screen on software and hardware issues to improve ...

## SearchContentManagement

• ### Test yourself on the differences between SharePoint and OneDrive

Businesses use Microsoft SharePoint and OneDrive capabilities for a number of reasons. Test your knowledge on the differences ...

• ### Akeneo rolls out newest version of PIM software

Akeneo PIM version 4.0 features digital asset manager, API-integrated connection modules and AI attribute mapping to better ...

• ### Get to know 4 workflow automation tools

Workflow automation can benefit businesses by making manual processes digital, giving employees more time to work on other tasks....

## SearchHRSoftware

• ### How real-time analytics can benefit HR

For analytics to be truly useful, it must be embedded into user workflows. Learn how HR tools are incorporating such approaches ...

• ### Human vs. AI in recruiting: Why both matter

Recruiters may consider using both the human element and the data brought in by intelligent tools if it can help them make better...

• ### Kronos-Ultimate Software merger creates \$3B firm with product overlap

Kronos Inc. and Ultimate Software are being merged by their private equity owner into a new, yet unnamed, entity. How this will ...

Close