Nissan 370Z Forum  

Set Theory + IT question

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

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


Like Tree2Likes
  • 1 Post By
  • 1 Post By

Reply
 
LinkBack Thread Tools Display Modes
Old 01-27-2013, 11:35 PM   #1 (permalink)
Base Member
 
Join Date: Sep 2012
Location: Hualien, Taiwan
Posts: 7
Drives: '13 Z Roadster MR7AT
Rep Power: 14
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   #2 (permalink)
Enthusiast Member
 
Join Date: Apr 2011
Location: Brooklyn Park, MN
Posts: 424
Drives: 04 se-r spec v, 370z
Rep Power: 16
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
Reply

Bookmarks

Thread Tools
Display Modes

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 08:22 AM.


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