Mismatched data between two tables and two fields

CodyMonster

Board Regular
Joined
Sep 28, 2009
Messages
159
I'm trying to get my head around how to find a situation where I might have a field where two tables match, but, where another field might not.
I'll try and explain this the best I can.
Table 1:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Lease ID[/TD]
[TD]Ticket Number[/TD]
[/TR]
[TR]
[TD]22045[/TD]
[TD]100542[/TD]
[/TR]
[TR]
[TD]22046[/TD]
[TD]100543[/TD]
[/TR]
[TR]
[TD]22047[/TD]
[TD]100544[/TD]
[/TR]
[TR]
[TD]22048[/TD]
[TD]100545[/TD]
[/TR]
</tbody>[/TABLE]








Table 2:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Lease ID[/TD]
[TD]Ticket Number[/TD]
[/TR]
[TR]
[TD]22045[/TD]
[TD]100542[/TD]
[/TR]
[TR]
[TD]22046[/TD]
[TD]100544[/TD]
[/TR]
[TR]
[TD]22047[/TD]
[TD]100543[/TD]
[/TR]
[TR]
[TD]22048[/TD]
[TD]100545[/TD]
[/TR]
</tbody>[/TABLE]









I'm having a hard time thinking of a way a query would spit out that 22046 doesn't have matching ticket numbers.
Maybe this would be easy for someone else. But I'm having a difficult time understanding how to do this.


Thanks for all the help everyone.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi Cody,

Could you pull both Ticket number fields into the query and then use an IIF statement to check they match?

IE: IIF([Table 1.Ticket Number]=[Table 2.Ticket Number],"Match","No Match")
 
Last edited by a moderator:
Upvote 0
Hi Cody,

Could you pull both Ticket number fields into the query and then use an IIF statement to check they match?

IE: IIF([Table 1.Ticket Number]=[Table 2.Ticket Number],"Match","No Match")


I think, tell me if I'm wrong here, they they would all match.
Because the ticket numbers match, they just belong to the wrong lease ID.

for example:
100543 is in both tables, just under two different lease ID's
 
Last edited:
Upvote 0
Code:
select 
  table_1.[Lease ID] 
from
 table_1
where 
(
  table_1.[Lease ID] 
  not in 
  (
    select 
      table_1.[Lease ID] 
    from 
      table_1
        inner join 
          table_2
            on
            table_1.[Lease ID] = table_2.[Lease ID]
            and 
            table_1.[Ticket Number] = table_2.[Ticket Number]
  )
)
 
Last edited:
Upvote 0
James,
Thanks.. But.. getting an Syntax error..

I changed the SQL to reflect what my database tables really are.. and it's the same as what you posted.

select
Truck_Tix.[Property ID]
from
Truck_Tix
where
(
Truck_Tix.[Property ID]
not in
(
select
Truck_Tix.[Property ID]
from
Truck_Tix
inner join
Restatements
on
Truck_Tix.[Property ID] = Restatement.Lease Number]
and
Truck_Tix.[Ticket Num] = Restatement.[Truck Ticket Num]
)
)
 
Upvote 0
Here is a second approach.

1) First get all Ticket Numbers from both tables (first Query)
2) Then find mismatches (second Query)


First query (Called Query4):
Code:
select [Ticket Num] 
from Truck_Tix
union
select [Truck Ticket Num]
from Restatements

Second Query (called Query5):
Code:
select 
	A.[Ticket Num], 
	B.[Property ID] as [Property ID - Truck_Tix], 
	C.[Lease Number] as [Lease Number - Restatements]
from 
	((Query4 A
	left join Truck_Tix B
	on A.[Ticket Num] = B.[Ticket Num])
	left join Restatements C
	on A.[Ticket Num] = C.[Truck Ticket Num])
where 
	nz(B.[Property ID], 1) <> nz(C.[Lease Number], 1)
	and B.[Property ID] is not null
	and C.[Lease Number] is not null

the last part in the where clause is only to make sure that nulls aren't compared (if you want a null in one table to count as a mismatch, or a null in both tables to also be included as a possible mismatch, then you can adjust or even omit the where clause).

the names of the queries are irrelevant of course (but the second one must use the correct name of the first one). Advise that you use actual table names and field names - and provide datatypes! - for quicker responses - otherwise we have to make the missing pieces ourselves.
 
Upvote 0

Forum statistics

Threads
1,223,992
Messages
6,175,832
Members
452,674
Latest member
psion2600

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top