I have table T1
ID STATUS
1 NEW
2 CLOSED
3 NEW
I have table T2
ID OWNER
1 A
1 B
1 C
1 A
1 B
2 A
2 B
2 A
3 A
I want to join T1 and T2 and to have multiple rows for every different ID and for every different OWNER. If OWNER is repeated multiple times in T2 that should be ignored and that duplicates should be removed.
So the final desired result should be:
ID STATUS OWNER
1 NEW A
1 NEW B
1 NEW C
2 CLOSED A
2 CLOSED B
3 NEW A
So you can see that duplicates (same owner for same ID multiple times) should be removed. I just need to have output as list of IDs and every different OWNER for that ID but it is not important how many times he was responsible in T2 table. I need to make somehow distinction.
If I do the query like this I am not getting distinct values per ID and OWNER but I am also having duplicates in this case.
select t1.id,t1.status,t2.owner
FROM t1
join t2
on t1.id=t2.id
Solution. Select column values in a specific order within rows to make rows with duplicate sets of values identical. Then you can use SELECT DISTINCT to remove duplicates. Alternatively, retrieve rows in such a way that near-duplicates are not even selected.
ALWAYS put the join predicates in the join. Where clause predicates are not evaluated until after the entire result set has been generated, so unneeded rows are carried along throughout the processing, and in certain outer join scenarios, putting predicates in where clause will generate incorrect results.
Again, if we perform a left outer join where date = date, each row from Table 5 will join on to every matching row from Table 4. However, in this case, the join will result in 4 rows of duplicate dates in the joined DataSet (see Table 6).
The SQL UNION ALL operator is used to combine the result sets of 2 or more SELECT statements. It does not remove duplicate rows between the various SELECT statements (all rows are returned). Each SELECT statement within the UNION ALL must have the same number of fields in the result sets with similar data types.
If I correctly understand, you need this:
select T1.ID, T1.STATUS, t.OWNER from T1
inner join(
select ID, OWNER from T2 group by ID, OWNER
) t
on T1.ID = t.ID
You can use distinct keyward in ur query to get desired result.
select distinct t1.id,t1.status,t2.owner
FROM t1
join t2
on t1.id=t2.id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With