question

ddsprasad avatar image
ddsprasad asked

compare 2 table records

I have 2 tables each table has 2 count of group by records , I need to compare those records are equal or not . table 1 result set after count(input),group by (input) input count 0 10 1 122 table 2 result set input count 0 10 1 122 I need to compare and see 2 tables has equal count. any thoughts ?
sqlcountrow-counts
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

This site runs off of votes. Please mark all helpful answers by clicking on the thumbs up next to those answers. If any one answer lead to a solution, please indicate that by clicking on the check mark next to that answer.
0 Likes 0 ·
DenisT avatar image
DenisT answered
select * from (table 1 result set after count(input),group by (input)) as T1 left outer join (table 2 result set) as T2 on T2.input = T1.input and T2.count = T1.count where T2 IS NULL -- the row is missing/not exact match in T2; where T2 IS NOT NULL -- the row is exact match; Keep in mind that the position of the tables in the LEFT JOIN matter! If you need to compare the existence of the rows in both tables equally, then use FULL OUTER JOIN. Is this what you're looking for?
2 comments
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

oops. We answered almost simultaneously!
0 Likes 0 ·
thank you!
0 Likes 0 ·
Phil Factor avatar image
Phil Factor answered
For your example, try this. It assumes that input is a unique value create table #TableA (input int primary key, [count] int) insert into #TableA values (0,10),(1,122),(3,10),(4,122) create table #TableB (input int primary key, [count] int) insert into #TableB values (0,10),(1,122),(3,20),(5,122) Select min(source) as source, input, [count] from (Select 'TableA' as source, input, [count] from #TableA union all Select 'TableB', input, [count] from #TableB)f group by input, [count] having count(*)=1 /* source input count ------ ----------- ----------- TableA 3 10 TableB 3 20 TableA 4 122 TableB 5 122 (4 row(s) affected) */
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

thank you .
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.