473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

SQL joins

Can
Hi all,

No matter how hard I've tried and researched, I just cannot figure out
how to do the following. I kindda think there must be an easy way.

I want to join two tables.
Table 1:
C1 C2 C3
A X1 Y
A X2 Z
A X3 Y

Table2:
C1 C2 C3
A N Y
A M Z
A N Y

So I do:
select i1.*, i2.* from Table1 i1
join Table2 i2
on i1.C3 = i2.C3

And I get

A X1 Y A M Y
A X3 Y A M Y
A X1 Y A N Y
A X3 Y A N Y
A X2 Z A N Z

But I want

A X3 Y A M Y
A X1 Y A N Y
A X2 Z A N Z

Is there a way to achieve this with SQL joins? Or any other way?
Thanks already...
Can
Jul 20 '05 #1
3 1218
MC
Please post DDL and inserts. When you do that, tell us what are the
additional requierements/filters. For example, why is this row 'invalid':
A X1 Y A M Y
how do you know you dont want that one and that you want this one:

A X1 Y A N Y

Using X1 as an example...

"Can" <ce***@hotmail.com> wrote in message
news:6d*************************@posting.google.co m...
Hi all,

No matter how hard I've tried and researched, I just cannot figure out
how to do the following. I kindda think there must be an easy way.

I want to join two tables.
Table 1:
C1 C2 C3
A X1 Y
A X2 Z
A X3 Y

Table2:
C1 C2 C3
A N Y
A M Z
A N Y

So I do:
select i1.*, i2.* from Table1 i1
join Table2 i2
on i1.C3 = i2.C3

And I get

A X1 Y A M Y
A X3 Y A M Y
A X1 Y A N Y
A X3 Y A N Y
A X2 Z A N Z

But I want

A X3 Y A M Y
A X1 Y A N Y
A X2 Z A N Z

Is there a way to achieve this with SQL joins? Or any other way?
Thanks already...
Can

Jul 20 '05 #2
What is the relationship between the two tables? Apparently the only
thing you have to join them on is C3 but that doesn't give us (or SQL
Server) enough information to solve your problem. How is SQL supposed
to know which row from Table1 to join to which SINGLE row in Table2?
You seem to be basing the result you want on an undefined order of
precedence in the rows to be joined but there is no column to tell us
that information and tables in SQL have no inherent order at all.

Also, I think you may have made an error in your sample data. There is
no (A,M,Y) row in Table2. This article explains the best way to
describe your problem to help others to help you:
http://www.aspfaq.com/etiquette.asp?id=5006
--
David Portas
SQL Server MVP
--

Jul 20 '05 #3
going a bit further, given the data you presented It seems there is an error
in your output as well.
Changing the data per david's comment as follows
C1 C2 C3
A X1 Y
A X2 Z
A X3 Y

Table2:
C1 C2 C3
A N Y
A M Z
A M Y -- changed N to M

Here is your original requested output
A X3 Y A M Y
A X1 Y A N Y
A X2 Z A N* Z

The row A,M,Z will match with A,X2,Z but you are asking for A,N,Z
assuming this is a mistake as well and changing to this
A X2 Z A M* Z

It is still not possible to restrict the rows on a natural alignment of
data... ie y=y.
you have to add something extra to the join to specify, when t1.c3 is Y only
join to t2 where c3's are equal
AND match X1 to N and X3 to M
This is do-able but Quite ugly

It might be better to add another column so you can spell out this relation
ship more naturally.
Kevin Ruggles

"David Portas" <RE****************************@acm.org> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Also, I think you may have made an error in your sample data. There is
no (A,M,Y) row in Table2. > -- David Portas
SQL Server MVP
--

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jgalzic | last post by:
Hi, I'm having trouble doing joins correctly on two tables. I've read up a lot about the different types of joins and tried lots of variations on inner, outer, and left joins with no avail....
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
4
by: Sri | last post by:
I am writing a download process in which i have a condition where i need to join four tables. Each table have lot of data say around 300000 recs. my question is when i am doing the joins on...
1
by: Prem | last post by:
Hi All Database Gurus, I am trying to write code which will produce all the possible valid queries, given tables and join information for tables. Right now i am just trying to construct all the...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
4
by: michaelnewport | last post by:
Greetings, I like to write my inner joins as below, but someone at work tells me its not as 'performant' as using the 'inner join' statement. Is this true ? Is there a better way to write it...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
2
by: narendra vuradi | last post by:
Hi I have a requirement where in i haev to convert the SQL from Oracle to the one which will run on the SQL server. in the Oracle Query i am doing multiple joins, between some 13 tables. and...
9
by: shapper | last post by:
Hello, I am used to SQL but I am starting to use LINQ. How can I create Left, Right and Inner joins in LINQ? How to distinguish the different joins? Here is a great SQL example:...
36
by: TC | last post by:
I've used Access for many years. Several times, I've encountered a bug which I refer to as the "Vanishing Joins" bug. When it happens, joins vanish randomly from queries. More specifically, all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.