Reputation: 235
I have two tables.They have the same data but from different sources. I would like to find all columns from both tables that where id in table 2 occurs more than once in table 1. Another way to look at it is if table2.id occurs only once in table1.id dont bring it back.
I have been thinking it would be some combination of group by and order by clause that can get this done but its not getting the right results. How would you express this in a SQL query?
Table1 | id | info | state | date | | 1 | 123 | TX | 12-DEC-09 | | 1 | 123 | NM | 12-DEC-09 | | 2 | 789 | NY | 14-DEC-09 | Table2 | id | info | state | date | | 1 | 789 | TX | 14-DEC-09 | | 2 | 789 | NY | 14-DEC-09 | Output |table2.id| table2.info | table2.state| table2.date|table1.id|table1.info|table1.state|table1.date| | 1 | 789 | TX | 14-DEC-09 | 1 | 123 | TX | 12-DEC-09 | | 1 | 789 | TX | 14-DEC-09 || 1 | 123 | NM | 12-DEC-09 |
Upvotes: 1
Views: 2399
Reputation: 55
I find this a much simpler way to do it:
select TableA.*,TableB.*
from TableA
inner join TableB
on TableA.id=TableB.id
where TableA.id in
(select distinct id
from TableA
group by id
having count(*) > 1)
Upvotes: 0
Reputation: 3034
First, I would suggest adding an auto-incrementing column to your tables to make queries like this much easier to write (you still keep your ID as you have it now for relational-mapping). For example:
Table 1:
TableID int
ID int
Info int
State varchar
Date date
Table 2:
TableID int
ID int
Info int
State varchar
Date date
Then your query would be really easy, no need to group, use CTEs, or row_over partitioning:
SELECT *
FROM Table2 T2
JOIN Table1 T1
ON T2.ID = T1.ID
JOIN Table1 T1Duplicate
ON T2.ID = ID
AND T1.TableID <> T1Duplicate.TableID
It's a lot easier to read. Furthermore, there are lots of scenarios where an auto-incrementing ID field is benefitial.
Upvotes: 0
Reputation: 413
If you using MSSQL try using a Common Table Expression
WITH cte AS (SELECT T1.ID, COUNT(*) as Num FROM Table1 T1
INNER JOIN Table2 T2 ON T1.ID = T2.ID
GROUP BY T1.ID
HAVING COUNT(*) > 1)
SELECT * FROM cte
INNER JOIN Table1 T1 ON cte.ID = T1.ID
INNER JOIN Table2 T2 ON cte.ID = T2.ID
Upvotes: 2