Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-03-02 : 17:27:50
|
I'm trying to do a comparison on 2 tables. I KNOW for a fact that table Z (7970) has 100 members NOT in table Y (7870). Unfortunately , I'm getting 8034 when I try to do the comparison. I'm certain that something is amiss here. I just don't know what.Please let me know what it could be.Thanks.. Here's the code:SELECT z.MemberName, z.MemberID, z.COBName, z.StartDate, z.TermDate, z.COBID, z.MedPrimary, y.MemberName, y.MemberID, y.COBName, y.StartDate, y.TermDate, y.COBID, y.MedPrimaryFROM [Fowler].[dbo].[z_COBMembr] z LEFT OUTER JOIN [Fowler].[dbo].[y_COBMembr] y ON (z.MemberID = y.MemberID) AND (z.COBID = y.COBID)Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2005-03-02 : 17:51:45
|
| Shows you all members from z that aren't in y:[CODE]SELECT z.MemberName, z.MemberID, z.COBName, z.StartDate, z.TermDate, z.COBID, z.MedPrimary, y.MemberName, y.MemberID, y.COBName, y.StartDate, y.TermDate, y.COBID, y.MedPrimaryFROM Fowler.dbo.z_COBMembr zLEFT JOIN Fowler.dbo.y_COBMembr yON ( z.MemberID= y.MemberID )AND ( z.COBID= y.COBID )WHERE y.MemberIDIS NULL;[/CODE]Shows you all members in either y or z that don't exist in the other one:[CODE]SELECT z.MemberName, z.MemberID, z.COBName, z.StartDate, z.TermDate, z.COBID, z.MedPrimary, y.MemberName, y.MemberID, y.COBName, y.StartDate, y.TermDate, y.COBID, y.MedPrimaryFROM Fowler.dbo.z_COBMembr zFULL JOIN Fowler.dbo.y_COBMembr yON ( z.MemberID= y.MemberID )AND ( z.COBID= y.COBID )WHERE y.MemberIDIS NULLOR z.MemberIDIS NULL;[/CODE] |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-03-02 : 17:52:51
|
Are you trying to find the 100 people?? or the matching people?I would be the reason you are getting 8034 is that you have multiple records the match on the join criteria. So 1 record in z may match 2 records y (so you would have 2 records for that case).Corey "If the only tool you have is a hammer, the whole world looks like a nail." - Mark Twain |
 |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-03-02 : 17:58:22
|
quote: Originally posted by lazerath Shows you all members from z that aren't in y:[CODE]SELECT....WHERE y.MemberIDIS NULL;[/CODE]Shows you all members in either y or z that don't exist in the other one:[CODE]SELECT ..........WHERE y.MemberIDIS NULLOR z.MemberIDIS NULL;[/CODE]
Thanks for taking a stab at it, Shane. However, I found ZERO (0)matching, running the first code set and only FOUR (4) running the second set of code.Hmmmmm...still missing another 96.... Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
 |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-03-02 : 17:59:23
|
quote: Originally posted by Seventhnight Are you trying to find the 100 people?? or the matching people?I would be the reason you are getting 8034 is that you have multiple records the match on the join criteria. So 1 record in z may match 2 records y (so you would have 2 records for that case).Corey "If the only tool you have is a hammer, the whole world looks like a nail." - Mark Twain
Uh I want the missing 100! Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
 |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2005-03-02 : 18:00:18
|
| Is COBID a foreign key to a Coordination of Benefits record? |
 |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-03-02 : 18:06:00
|
quote: Originally posted by lazerath Is COBID a foreign key to a Coordination of Benefits record?
Wow! You know? Yes, COB is Coordination-of-Benefit! Actually I haven't set up any pk/fk-sSemper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
 |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2005-03-02 : 18:12:00
|
| Xerxes,I am deeply involved in the insurance world, so I feel your pain dealing with the data involved. Most places violate every known best practice there is concerning relational data.As far as those queries are concerned, I am confused. Did you copy my queries exactly or did you modify them? They should work perfectly. |
 |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-03-02 : 18:16:43
|
quote: Originally posted by lazerath Xerxes,I am deeply involved in the insurance world, so I feel your pain dealing with the data involved. Most places violate every known best practice there is concerning relational data.As far as those queries are concerned, I am confused. Did you copy my queries exactly or did you modify them? They should work perfectly.
Thanks, but I'm the medical bureaucracy field. I did run both sets in SQA , but I got 0 & 4 . Would you recommend pks or fks?Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
 |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2005-03-02 : 18:19:21
|
Ah ha. I've got it. Corey is right. You have a serious duplicate problem. Try this:SELECT COUNT(*)AS UniqueMemberCount, SUM ( CASE WHEN z.MemberID IS NOT NULL AND y.MemberID IS NOT NULL THEN 1 ELSE 0 END )AS MatchingRecords, SUM ( CASE WHEN z.MemberID IS NULL THEN 1 ELSE 0 END )AS OnlyInTableY, SUM ( CASE WHEN y.MemberID IS NULL THEN 1 ELSE 0 END )AS OnlyInTableZFROM ( SELECT DISTINCT tz.MemberID, tz.COBID FROM Fowler.dbo.z_COBMembr tz ) zFULL JOIN ( SELECT DISTINCT ty.MemberID, ty.COBID FROM Fowler.dbo.y_COBMembr ty ) yON ( z.MemberID= y.MemberID )AND ( z.COBID= y.COBID ); Please post the results. |
 |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2005-03-02 : 18:23:20
|
quote: Originally posted by Xerxes Thanks, but I'm the medical bureaucracy field. I did run both sets in SQA , but I got 0 & 4 . Would you recommend pks or fks?Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL!
Its all different ends of the same industry, really. :-) |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-03-02 : 19:05:10
|
>> Would you recommend pks or fks?You must be kiding Xerxes, right ?YES!They will prevent duplicate data and other data inconsistencies in the database.(not after the fact though)Clear up the duplicates first, then run your comparisons again.rockmoose |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-03-02 : 19:36:03
|
quote: Would you recommend pks or fks
Please tell me that this isn't a production database.So how are you handling referential integrity? You are going to say the application handles it, right? Do you have a DBA there to slap you for me? Tara |
 |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-03-02 : 20:34:07
|
quote: Originally posted by tduggan
quote: Would you recommend pks or fks
Please tell me that this isn't a production database.So how are you handling referential integrity? You are going to say the application handles it, right? Do you have a DBA there to slap you for me? Tara
Oh, Great SQL Warrior Queen , I've been working with SQL for 6 months (not including my work with SAS---see:Proc SQL) on SQL Server 2000. My DBA is a faceless amorphous form--contractor type--who knows only what I tell him/her/it/whatever. So I'm kinda the DBA . I have all administration rights, and all the server volume I need. Unfortunately , I don't have the expertise you have. That's why I'm a frequent pest here at TeamSQL.com. I learn more than you might believe from folks like yourself who are willing to assist this 30-year IT veteran. I'm converting an old dBase5 system to SQL--into DTS packages ("tied up with string!"). I'm bogged down with running the old system while writing the new. Multitasking is a normal format for me (as it is for most Marines). But I really could use two things from you: 1.) your help and 2.) the benefit of the doubt. So, uh, would you KINDLY clue me in? I'd like to be the kind of 'Yak' you are---although "Warrior Queen" really wouldn't apply in my case--maybe "Warrior Emperor"--but that's a few thousand posts from now. Feel free to 'smack' me whenever, just include the information, too! Now where were we?Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-03-02 : 21:13:22
|
| Well this isn't a SQL Server specific concept, that is referential integrity. I'd suggest getting a book or two on fundamentals of database design. I don't have any recommendations, but I think some people here have recommended one that's called Admin 101 or something like that.Tara |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-03-03 : 05:52:46
|
xerx frist do this:it will give you the records that are duplicated.select z.MemberName, z.MemberID, z.COBName, z.StartDate, z.TermDate, z.COBID, z.MedPrimary,from [Fowler].[dbo].[z_COBMembr] zgroup by z.MemberName, z.MemberID, z.COBName, z.StartDate, z.TermDate, z.COBID, z.MedPrimaryhaving count(*)>1select y.MemberName, y.MemberID, y.COBName, y.StartDate, y.TermDate, y.COBID, y.MedPrimaryfrom [Fowler].[dbo].[y_COBMembr] y group by y.MemberName, y.MemberID, y.COBName, y.StartDate, y.TermDate, y.COBID, y.MedPrimaryhaving count(*)>1 then go on from there...Go with the flow & have fun! Else fight the flow |
 |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-03-03 : 09:46:24
|
quote: Originally posted by spirit1 xerx frist do this:it will give you the records that are duplicated.select z.MemberName, z.MemberID, z.COBName, z.StartDate, z.TermDate, z.COBID, z.MedPrimary,from [Fowler].[dbo].[z_COBMembr] zgroup by z.MemberName, z.MemberID, z.COBName, z.StartDate, z.TermDate, z.COBID, z.MedPrimaryhaving count(*)>1select y.MemberName, y.MemberID, y.COBName, y.StartDate, y.TermDate, y.COBID, y.MedPrimaryfrom [Fowler].[dbo].[y_COBMembr] y group by y.MemberName, y.MemberID, y.COBName, y.StartDate, y.TermDate, y.COBID, y.MedPrimaryhaving count(*)>1 then go on from there...Go with the flow & have fun! Else fight the flow 
Thanks, Mlad! .....but I got 0 & 0 for both results...... Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-03-03 : 09:49:57
|
| I gotta ask...30 year IT Vet...What does IT stand for?Brett8-) |
 |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-03-03 : 09:52:47
|
quote: Originally posted by lazerath
quote: Originally posted by Xerxes Thanks, but I'm the medical bureaucracy field. I did run both sets in SQA , but I got 0 & 4 . Would you recommend pks or fks?Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL!
Its all different ends of the same industry, really. :-)
Tried your code, Laz......here's the results:UniqueMemberCount MatchingRecords OnlyInTableY OnlyInTableZ ----------------- --------------- ------------ ------------ 7945 7941 4 0(1 row(s) affected)Thanks so much for your assistance. I guess I'll need to find another way to detect those missing 100 .Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-03-03 : 10:14:45
|
ok this is intriguing... i wish you could post the data so we could take a look at it....Go with the flow & have fun! Else fight the flow |
 |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-03-03 : 10:26:22
|
I'm going to check out the 50 first records from each and see if I can determine what anomaly is causing this. I promise to report on my findings! Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
 |
|
|
Next Page
|
|
|
|
|