Skip to content
Advertisement

Using Subqueries to remove duplicate IDs [closed]

I have 2 Tables. Table 1 holds ID1 and ID2. Table 2 holds ID2 and ID3.

Table 1 has unique cases for ID1 and multiple cases for ID2.

Table 2 has unique cases for ID2 and multiple cases for ID3

I want 1 unique case of ID3.

I need remove duplicate ID2s from Table 1 picking to remove the duplicate ID2s based on the smaller ID1

So Table 1 now looks like:

Now I want to go to Table 2 and remove any duplicate ID3s based on the smaller ID2

So my end result should be (I am joining the tables because both of them have other relevant information I need to combine but these are the IDs I am sorting and filtering to get the correct row):

Where now I have a single case for each ID3 based on the largest ID1 and ID2 associated with that ID3.

I have tried creating subqueries in the WHERE function to remove the duplicates but my understanding of SQL is not good enough to really figure out what is happening.

Group By and DISTINCT does not work for this case.

Decision Tree

I added a Decision Tree to help visualize the problem. Essentially, each ID3 can potentially have multiple ID2s, which can potentially have multiple ID1s. I want to keep only the largest ID1, which gives me the correct ID2 associated with that ID3.

Advertisement

Answer

ID1 ID2 ID3
2 2 1
7 5 2
5 4 3
10 6 4
9 7 5

Fiddle

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement