Nissan 370Z Forum  

Set Theory + IT question

Give me some ideas IT guys. I'm stuck trying to figure this out on a Sunday!!! Given set A and set B where (A != B) Set A is given

Go Back   Nissan 370Z Forum > Nissan 370Z General Area > The Lounge (Off Topic)


Like Tree2Likes
  • 1 Post By 6MT
  • 1 Post By hansoac

Reply
 
LinkBack Thread Tools Display Modes
Old 01-27-2013, 04:55 PM   #1 (permalink)
Enthusiast Member
 
Join Date: Apr 2011
Location: Brooklyn Park, MN
Posts: 424
Drives: 04 se-r spec v, 370z
Rep Power: 14
homeryansta will become famous soon enough
Default Set Theory + IT question

Give me some ideas IT guys. I'm stuck trying to figure this out on a Sunday!!!

Given set A and set B where (A != B)

Set A is given in comma delimited form:
ID,Value
---------
1,1
1,2
2,2
3,1

Set B is given in comma delimited form:
ID,Value2
----------
1,1
1,3
1,4
2,2
3,1
3,2

I want the result set of A and B merged to look like this

ID,Value,Value2
------------------
1,1,1
1,2,3
1,null,4
2,2,2
3,1,1
3,null,2

Notice that I want to join the ID in the order they are displayed.
If I do a "full outer join" on the ID, I get 6 results for ID 1 which is not what I want.
If I do a union on both sets with dummy columns to match them up, I will still get 5 results for ID 1.

Any ideas? I can barely think anymore.



The IDs have to match, but doesn't cross match like in a traditional join.

Example:
if A has 3 rows with ID 1
and B has 1 row with ID 1

match the first row in A to the only row in B
and then just display the rest of A.

In a traditional join, all 3 rows in A would be join to the single row in B.
__________________
homeryansta is offline   Reply With Quote
Old 01-27-2013, 05:28 PM   #2 (permalink)
XiP
A True Z Fanatic
 
XiP's Avatar
 
Join Date: Dec 2011
Location: no clipping
Posts: 11,096
Drives: ↑ ↓
Rep Power: 533
XiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond repute
Default

Are you trying to do this in SQL?
XiP is offline   Reply With Quote
Old 01-27-2013, 05:32 PM   #3 (permalink)
Enthusiast Member
 
Join Date: Apr 2011
Location: Brooklyn Park, MN
Posts: 424
Drives: 04 se-r spec v, 370z
Rep Power: 14
homeryansta will become famous soon enough
Default

Quote:
Originally Posted by XiP View Post
Are you trying to do this in SQL?
yes, SQL server
__________________
homeryansta is offline   Reply With Quote
Old 01-27-2013, 05:46 PM   #4 (permalink)
XiP
A True Z Fanatic
 
XiP's Avatar
 
Join Date: Dec 2011
Location: no clipping
Posts: 11,096
Drives: ↑ ↓
Rep Power: 533
XiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond reputeXiP has a reputation beyond repute
Default

You need to do a right outer join on the ID
XiP is offline   Reply With Quote
Old 01-27-2013, 05:59 PM   #5 (permalink)
Enthusiast Member
 
Join Date: Apr 2011
Location: Brooklyn Park, MN
Posts: 424
Drives: 04 se-r spec v, 370z
Rep Power: 14
homeryansta will become famous soon enough
Default

Quote:
Originally Posted by XiP View Post
You need to do a right outer join on the ID
That won't work if the left set has data that doesn't exist in the right set.

The result I want is a 1-1 relationship where the matching only happens in order

example:

if A{1,1,1,2,2,3,3,3} and B{1,1,2,4}

A B
-- --
1 1
1 1
1 null
2 2
2 null
3 null
3 null
3 null
null 4
__________________

Last edited by homeryansta; 01-27-2013 at 06:03 PM.
homeryansta is offline   Reply With Quote
Old 01-27-2013, 11:35 PM   #6 (permalink)
Base Member
 
Join Date: Sep 2012
Location: Hualien, Taiwan
Posts: 7
Drives: '13 Z Roadster MR7AT
Rep Power: 12
Little Blue is on a distinguished road
Default

This is with MS Access SQL, but SQL Server should be very similar. I struggled with this a lot the first time I needed to do it years ago. The trick is to build a set of all master combinations with a UNION, then use that as the base for the Left Joins.

SELECT x.Id, a.Value, b.Value
FROM ((
SELECT Id, Value
FROM A
UNION
SELECT Id, Value
FROM B
) as x
left join a on x.id = a.id and x.value = a.value)
left join b on x.id = b.id and x.value = b.value

Id a.Value b.Value
1 1 1
1 2
1 3
1 4
2 2 2
3 1 1
3 2
Little Blue is offline   Reply With Quote
Old 01-30-2013, 11:33 PM   #7 (permalink)
Enthusiast Member
 
Join Date: Apr 2011
Location: Brooklyn Park, MN
Posts: 424
Drives: 04 se-r spec v, 370z
Rep Power: 14
homeryansta will become famous soon enough
Default

Quote:
Originally Posted by Little Blue View Post
This is with MS Access SQL, but SQL Server should be very similar. I struggled with this a lot the first time I needed to do it years ago. The trick is to build a set of all master combinations with a UNION, then use that as the base for the Left Joins.

SELECT x.Id, a.Value, b.Value
FROM ((
SELECT Id, Value
FROM A
UNION
SELECT Id, Value
FROM B
) as x
left join a on x.id = a.id and x.value = a.value)
left join b on x.id = b.id and x.value = b.value

Id a.Value b.Value
1 1 1
1 2
1 3
1 4
2 2 2
3 1 1
3 2

That won't work. The reason is because the solution I want is a 1-1 relationship between a m-m set.

Here is how I solved it.

I partitioned the IDs and assigned unique keys(SubID) to them..

That is:

ID,Value2,SubID
----------
1,1,1
1,3,2
1,4,3
2,2,1
3,1,1
3,2,2

I do the same thing to the other set.

The resulting set after I do a full outer join on ID and SubID is exactly what I wanted.

Thanks for the help folks!
__________________
homeryansta is offline   Reply With Quote
Old 01-31-2013, 09:07 AM   #8 (permalink)
6MT
A True Z Fanatic
 
6MT's Avatar
 
Join Date: May 2009
Location: ITU Zone 2
Posts: 22,459
Drives: Reliant Robin
Rep Power: 1789
6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute
Default

1st likes this.
__________________
Andrew

2009 370Z Sport/Touring 6MT sold
2013 Honda Civic Si coupe D/D
6MT is offline   Reply With Quote
Old 01-31-2013, 09:14 AM   #9 (permalink)
Base Member
 
Staggered357's Avatar
 
Join Date: May 2012
Location: NY VA GA
Posts: 149
Drives: Nissan Maxima 10
Rep Power: 12
Staggered357 is on a distinguished road
Default

WTF squared..
My head hurts just looking at it... My hats goes off to you programmers... I will stick with the technical support and subnetting
Staggered357 is offline   Reply With Quote
Old 01-31-2013, 09:15 AM   #10 (permalink)
6MT
A True Z Fanatic
 
6MT's Avatar
 
Join Date: May 2009
Location: ITU Zone 2
Posts: 22,459
Drives: Reliant Robin
Rep Power: 1789
6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute6MT has a reputation beyond repute
Default

Quote:
Originally Posted by Staggered357 View Post
WTF squared..
My head hurts just looking at it... My hats goes off to you programmers... I will stick with the technical support and subnetting
Ahh.... a cable swapper, are ya?
__________________
Andrew

2009 370Z Sport/Touring 6MT sold
2013 Honda Civic Si coupe D/D
6MT is offline   Reply With Quote
Old 01-31-2013, 10:00 AM   #11 (permalink)
Enthusiast Member
 
Join Date: Apr 2011
Location: Brooklyn Park, MN
Posts: 424
Drives: 04 se-r spec v, 370z
Rep Power: 14
homeryansta will become famous soon enough
Default

Quote:
Originally Posted by Staggered357 View Post
WTF squared..
My head hurts just looking at it... My hats goes off to you programmers... I will stick with the technical support and subnetting
we need guys like you to break the network so we have down times.
__________________
homeryansta is offline   Reply With Quote
Old 01-31-2013, 03:08 PM   #12 (permalink)
Base Member
 
Staggered357's Avatar
 
Join Date: May 2012
Location: NY VA GA
Posts: 149
Drives: Nissan Maxima 10
Rep Power: 12
Staggered357 is on a distinguished road
Default

Definitely!!!
Staggered357 is offline   Reply With Quote
Old 02-01-2013, 06:29 AM   #13 (permalink)
Enthusiast Member
 
hansoac's Avatar
 
Join Date: Feb 2011
Location: Toledo
Posts: 497
Drives: 09 370z Sport CY
Rep Power: 14
hansoac is on a distinguished road
Default

I started out my degree as CS, but ended up changing to MIS because I could not stand sitting and reading/writing code all day. However, as I age I started getting back into some programming and seem to enjoy it.

I am in an iOS Objective-C class right now, tough stuff!
marks370z likes this.
hansoac is offline   Reply With Quote
Old 02-01-2013, 09:28 AM   #14 (permalink)
Enthusiast Member
 
Join Date: Apr 2011
Location: Brooklyn Park, MN
Posts: 424
Drives: 04 se-r spec v, 370z
Rep Power: 14
homeryansta will become famous soon enough
Default

Quote:
Originally Posted by hansoac View Post
I started out my degree as CS, but ended up changing to MIS because I could not stand sitting and reading/writing code all day. However, as I age I started getting back into some programming and seem to enjoy it.

I am in an iOS Objective-C class right now, tough stuff!
sitting, reading, writing all day? What kind of cs ******** program did your school have? I would think MIS is more of the worthless sitting, reading, writing you mentioned.

I was programming games with AI, 3D graphics at a very low level, rewriting the memory management system in an OS. Things that are actually interesting.
__________________
homeryansta is offline   Reply With Quote
Reply

Bookmarks


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie Question... smart key question ellebycul New Forum Member 3 05-01-2011 05:04 PM


All times are GMT -5. The time now is 11:42 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.6.0 PL2