![]() |
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
|
![]() |
|
LinkBack | Thread Tools | Display Modes |
![]() |
#1 (permalink) |
Enthusiast Member
Join Date: Apr 2011
Location: Brooklyn Park, MN
Posts: 424
Drives: 04 se-r spec v, 370z
Rep Power: 14 ![]() |
![]()
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.
__________________
![]() |
![]() |
![]() |
![]() |
#5 (permalink) |
Enthusiast Member
Join Date: Apr 2011
Location: Brooklyn Park, MN
Posts: 424
Drives: 04 se-r spec v, 370z
Rep Power: 14 ![]() |
![]()
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 07:03 PM. |
![]() |
![]() |
![]() |
#6 (permalink) |
Base Member
Join Date: Sep 2012
Location: Hualien, Taiwan
Posts: 7
Drives: '13 Z Roadster MR7AT
Rep Power: 13 ![]() |
![]()
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 |
![]() |
![]() |
![]() |
#7 (permalink) | |
Enthusiast Member
Join Date: Apr 2011
Location: Brooklyn Park, MN
Posts: 424
Drives: 04 se-r spec v, 370z
Rep Power: 14 ![]() |
![]() Quote:
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!
__________________
![]() |
|
![]() |
![]() |
![]() |
#13 (permalink) |
Enthusiast Member
Join Date: Feb 2011
Location: Toledo
Posts: 497
Drives: 09 370z Sport CY
Rep Power: 15 ![]() |
![]()
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! |
![]() |
![]() |
![]() |
#14 (permalink) | |
Enthusiast Member
Join Date: Apr 2011
Location: Brooklyn Park, MN
Posts: 424
Drives: 04 se-r spec v, 370z
Rep Power: 14 ![]() |
![]() Quote:
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.
__________________
![]() |
|
![]() |
![]() |
![]() |
Bookmarks |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Newbie Question... smart key question | ellebycul | New Forum Member | 3 | 05-01-2011 06:04 PM |