Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

find out which record_ids match on 2 fields and not match a third field. Expand / Collapse
Author
Message
Posted Tuesday, February 12, 2008 1:07 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 7:59 AM
Points: 1,946, Visits: 1,717
I need to match up information from 2 different sources.

one table has a record_ID used to track results.
It is inner joined to second source, creating the data below.

I can classify the data as matched, unmatched and multiple matches.

Now I need to classify the multiple matches further.

How do if find out which record_ids match on 2 fields and not match a third field. With any other row with same record_id and update status for that row.

The first three rows meet the criteria where S_desc and M_desc match , but not Item
The next three rows fails because all three row match on Item, S_desc and M_desc.

Then update the status in a status table with one to one mapping

CREATE TABLE [dbo].[#tempCheck](
[tempcheck_id] [numeric](10,0) null,
[RECORD_ID] [numeric](10, 0) NULL,
[Item] varchar(15) NULL,
[I_desc] varchar(24) null,
[S_desc] varchar(50) null,
[M_desc] varchar(10) null )


insert into #tempcheck
values ('1', '1','123','desc','blue','rg')
insert into #tempcheck
values ( '2','1','124','desc','blue','rg')
insert into #tempcheck
values ( '3','1','125','desc','blue','rg')
insert into #tempcheck
values ( '4','2','123','desc','blue','rg')
insert into #tempcheck
values ( '5','2','123','desc','blue','rg')
insert into #tempcheck
values ( '6','2','123','desc','blue','rg')
insert into #tempcheck
values ( '7','3','123','desc','blue','rg')
insert into #tempcheck
values ( '8','3','123','desc','blue','rg')
insert into #tempcheck
values ( '9','3','123','desc','blue','rg')
insert into #tempcheck
values ( '10','3','123','desc','blue','rg')
insert into #tempcheck
values ( '11','4','123','desc2','blue','rg')
insert into #tempcheck
values ( '12','4','123','desc2','blue','rg')
insert into #tempcheck
values ( '13','4','123','desc3','blue','rg')
insert into #tempcheck
values ( '14','4','123','desc3','blue','rg')



CREATE TABLE [dbo].[#tempflag](
[tempcheck_id] [numeric](10,0) null,
[RECORD_ID] [numeric](10, 0) NULL,
[Status_Flag] varchar(15) NULL)

insert into #tempflag
(tempcheck_id, RECORD_ID, Status_Flag)
select tempcheck_id, RECORD_ID, 'Not_Matched' as Status_Flag
From #tempcheck

select tempcheck_id, RECORD_ID, Status_Flag
From #tempflag

drop table #tempcheck
drop table #tempflag

Thanks for your assistance.
Ben
Post #454661
Posted Tuesday, February 12, 2008 1:41 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 9:58 AM
Points: 13,872, Visits: 9,600
Try something like this:

select distinct tempcheck_id, record_id
from #tempcheck
inner join
(select tempcheck_id, min(record_id) as min_id
from #tempcheck
group by tempcheck_id) Sub
on main.tempcheck_id = sub.tempcheck_id
and main.record_id = sub.min_id
except
select t1.tempcheck_id, t1.record_id
from #tempcheck t1
inner join #tempcheck t2
on t1.record_id = t2.record_id
and t1.tempcheck_id < t2.tempcheck_id
and t1.item = t2.item
and t1.i_desc = t2.i_desc
and t1.s_desc = t2.s_desc
and t1.m_desc = t2.m_desc
inner join #tempcheck t3
on t1.record_id = t3.record_id
and t2.tempcheck_id < t3.tempcheck_id
and t1.item = t3.item
and t1.i_desc = t3.i_desc
and t1.s_desc = t3.s_desc
and t1.m_desc = t3.m_desc

You may need to play around with the join criteria to get exactly what you want, but it should put you in the right direction.

I noticed you have four of some record IDs. The above code will find if any three of them match, ignoring and unmatched fourth record. If you want to include unmatched fourth records, create a second query that extends the joins by one.


- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #454688
Posted Tuesday, February 12, 2008 1:45 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 8:26 AM
Points: 10,381, Visits: 13,436
Can you post what you want the final result to look like?



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #454694
Posted Tuesday, February 12, 2008 3:16 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 7:59 AM
Points: 1,946, Visits: 1,717
End result is a report show the type for each row.

tempcheck_id RECORD_ID Item I_desc S_desc M_desc Status_Flag
1 1 123 desc blue rg Type1
2 1 124 desc blue rg Type1
3 1 125 desc blue rg Type1
4 2 123 desc blue rg Type2
5 2 123 desc blue rg Type2
6 2 123 desc blue rg Type2
7 3 123 desc blue rg Type2
8 3 123 desc blue rg Type2
9 3 123 desc blue rg Type2
10 3 123 desc blue rg Type2
11 4 123 desc2 blue rg Type3
12 4 123 desc2 blue rg Type3
13 4 123 desc3 blue rg Type3
14 4 123 desc3 blue rg Type3


Where Type1 I_desc and M_desc match and Item is not, for same for record_Id.
Where Type2 I_desc and M_Desc and Item all match for same record_ID.
Where Type3 I_desc is not matched and M_Desc and Item are matched, for same record_id.
Post #454765
Posted Wednesday, February 13, 2008 6:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 30, 2008 6:11 AM
Points: 8, Visits: 10
I won't say that this is the best way to do it, but I believe a simple self join on record_id with a case statement will work:

select 
A.*,
B.*,
Case
When A.item = B.item and A.I_desc = B.I_desc and A.m_desc = B.m_desc then 'Type 2'
When A.I_desc = B.I_desc and A.m_desc = B.m_desc then 'Type 1'
When A.item = B.item and A.m_desc = B.m_desc then 'Type 3'
Else 'No Match'
End as status_flag
From
#tempcheck A join
#tempcheck B On
A.record_id = B.record_id And
A.tempcheck_id > B.tempcheck_id
Order by A.tempcheck_id

Post #455002
Posted Wednesday, February 13, 2008 7:56 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:28 AM
Points: 7,179, Visits: 15,775
Do you care which of the three doesn't match? If not - you could use JUST the "matches" part below

;with JoinMath as (
Select a.recordid, a.tempcheckid,
case when A.I_desc = B.I_desc then 1 else 0 end +
case when A.m_desc = B.m_desc then 1 else 0 end +
case when A.item = B.item then 1 else 0 end as Matches,
--create a pseudo bitbucket
case when A.I_desc = B.I_desc then 1 else 0 end +
case when A.m_desc = B.m_desc then 2 else 0 end +
case when A.item = B.item then 1 else 0 end as MatchFilter
From #tempcheck a
inner join #tempcheck b on a.recordID=b.recordID and a.tempcheckID<>a.tempcheckid
)
Select *,
case
when MatchFilter & 1 = 0 then 'Type1'
when MatchFilter & 2 = 0 then 'Type4'
when MatchFilter & 4 = 0 then 'Type3'
ELSE 'Type2'
end as MatchType
from JoinMath
where Matches>1



----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #455088
Posted Wednesday, February 13, 2008 9:59 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 8:26 AM
Points: 10,381, Visits: 13,436
Matt's solution is more efficient than mine. I thought about using a CTE, but have not used them enough to be comfortable. If you set statustics IO on my solution returns:

Scan count 2, logical reads 15 physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

And Matt's:

Scan count 2, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.





Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #455195
Posted Wednesday, February 13, 2008 11:17 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, October 30, 2014 10:24 AM
Points: 99, Visits: 640
I know this isn't the question that was asked, but...

Is there a reason this couldn't be run from the two master tables, skipping the #Temp table?

Edit: This question was really for the OP, not all the magnanimous responders that were helping him out. ;)



Post #455250
Posted Wednesday, February 13, 2008 11:24 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Thursday, December 18, 2014 8:26 AM
Points: 10,381, Visits: 13,436
I just assumed the temp table represented the master table for example and testing purposes.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #455254
Posted Wednesday, February 13, 2008 11:56 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 7:28 AM
Points: 7,179, Visits: 15,775
Agreed - no reason it couldn't be run against the master, but since OP gave us a temp table, that's what I used.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #455273
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse