Show Duplicates Across 2 Queries

QandAdam

New Member
Joined
Jan 12, 2019
Messages
30
I have 2 queries:

Both Queries have the same fields and are based on the same table (tblBusinesses) but have different criteria

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Query Name[/TD]
[TD]Criteria[/TD]
[/TR]
[TR]
[TD]OpenBusinesses[/TD]
[TD]'Status' field equals 'Open'[/TD]
[/TR]
[TR]
[TD]ClosedBusinesses[/TD]
[TD]'Status' field is not equal to 'Open'[/TD]
[/TR]
</tbody>[/TABLE]

Is there a way to find records which appear in both queries? The Duplicate field will be the 'RegNo' within the table.

Basically, I'm trying to find any Duplicate Businesses Records which have different Statuses. If there is a better way of doing this then I'm all ears!

Thanks in advance!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Remove status field, group on RegNo and add a count with criteria > 1 ?

HTH
 
Upvote 0
Remove status field, group on RegNo and add a count with criteria > 1 ?

HTH

How would this filter to only show the duplicate RegNos with different statuses?

E.g.
Instead of this:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]RegNo[/TD]
[TD]Status[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]Active[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]Closed[/TD]
[/TR]
[TR]
[TD]456[/TD]
[TD]Closed[/TD]
[/TR]
[TR]
[TD]456[/TD]
[TD]Closed[/TD]
[/TR]
</tbody>[/TABLE]

I want it to look like this:
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]RegNo[/TD]
[TD]Status[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]Active[/TD]
[/TR]
[TR]
[TD]123[/TD]
[TD]Closed[/TD]
[/TR]
</tbody>[/TABLE]

RegNo '456' is ignored because although it is a duplicate RegNo, the Status on both records is 'Closed'
 
Last edited:
Upvote 0
Distinct, then count ...
Code:
select count(RegNo) as Total
from
(
select distinct RegNo, Status
from [Businesses]
) T
having count(T.RegNo) > 1


Another way (not necessarily distinct if there are even three or more records, some open others closed):
Code:
select t1.RegNo from [Businesses] t1 
where 
    t1.Status = 'Open' 
and exists (select * from [Businesses] t2 where t2.RegNo = t1.RegNo and t2.Status = 'Closed')
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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