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.

This was last published in February 2006

## Content

Find more PRO+ content and other member only offers, here.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Hyperledger Fabric offers path to enterprise blockchain future

Blockchain arose from bitcoin, but it's looking to find a place in the enterprise. Frameworks like Hyperledger Fabric could ...

• ### MongoDB 4.0 takes ACID transactions to multi-document level

MongoDB is taking a deeper step into SQL-style processing waters with a 4.0 update that brings increased support for ...

• ### Data lake concept needs firm hand to pay big data dividends

Data lakes pose technology deployment and data management challenges that can leave analytics users high and dry if the ...

• ### AI functionality limited today but could be a game-changer

Limited AI capabilities could soon give way to technology that is truly transformative for enterprises, surpassing the overhyped ...

## SearchSAP

• ### Avoiding SAP indirect access woes requires good faith

Some customers are concerned that SAP will hit them for indirect access licensing fees, but they can avoid trouble if they act in...

• ### ControlPanelGRC app eases Steelcase's compliance pain

When Steelcase's SAP environment grew in size and complexity, it turned to Symmetry ControlPanelGRC to save time, have more ...

• ### Translytical data platforms emerge with SAP HANA as a leader

SAP HANA is a leading translytical platform, according to Forrester, and consulting firm Convergent IS says the combination of ...

## SearchSQLServer

• ### Microsoft SQL Operations Studio eases SQL Server admin tasks

SQL Operations Studio simplifies routine administration of SQL Server and Azure SQL databases, making database development and ...

• ### Meltdown and Spectre fixes eyed for SQL Server performance issues

Microsoft has responded to the Spectre and Meltdown chip vulnerabilities with patches and other fixes. But IT teams need to sort ...

• ### Five SQL Server maintenance steps you should take -- ASAP

Putting off SQL Server administration tasks can lead to database problems. Enact these often-neglected maintenance items to help ...

## TheServerSide.com

• ### Pluralsight IQ, Stack Overflow boost developer street cred

Tying the Pluralsight IQ skills test to the Stack Overflow Developer Story helps developers measure their technical skills and ...

• ### Why this quantum computing breakthrough is a security risk

Quantum computing will void pretty much all security encryption techniques and open the door to hackers. Here's how to protect ...

• ### Database automation drives DevOps into the persistence layer

A new breed of software tools is driving DevOps processes deep into the persistence layer, as database automation and continuous ...

## SearchDataCenter

• ### Evaluate read-intensive and write-intensive SSD use cases

Consider write wear, performance and other factors when choosing between read-intensive, write-intensive and mixed-use ...

• ### Some hyper-converged infrastructure use cases pose pitfalls

Hyper-converged infrastructure adoption is skyrocketing, but that doesn't mean that the technology is the best choice for every ...

• ### Dell hyper-converged reorg streamlines products, ups CI odds

Market pressures and manufacturing synergies drove Dell to integrate its HCI and CI products with its core business units, but ...

## SearchContentManagement

• ### Content management in the cloud a main theme in 2018

The future of content management resides in the cloud and with AI, as several 2018 conferences will assure you.

• ### Six things to know about today's SharePoint implementations

As companies migrate their on-premises Microsoft SharePoint sites to the cloud, here are some things they should know about the ...

• ### Upgrades for the SharePoint Online portal

As more organizations migrate SharePoint sites to the cloud, Microsoft has increased at-a-glance dashboard data and analytics to ...

## SearchHRSoftware

• ### Limeade releases new employee engagement software

HR tech news roundup: Employee engagement software vendor Limeade expands platform and releases inclusion module for large ...

• ### Don't overlook the many benefits of Microsoft Excel for HR

The maligned spreadsheet tool is no substitute for enterprise apps like HRMS and people analytics, but it will do in a pinch and ...

• ### HR is failing to use people analytics tools, new report says

Human resource departments fail to use people analytics tools effectively, a new global study concludes. The findings were called...

Close