473,396 Members | 1,968 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,396 software developers and data experts.

Problem with inner join

Hello,
I have the follow table named A:
Expand|Select|Wrap|Line Numbers
  1. id  medicine
  2. 1   1
  3. 1   2
  4. 1   3
i wish to get the pairs of drugs that belong to the same id , for example:
Expand|Select|Wrap|Line Numbers
  1. medicine1 medicine2
  2. 1         2
  3. 1         3
  4. 2         3
i try to do this with this query:
Expand|Select|Wrap|Line Numbers
  1. select a1.medicine as medicine1, a2.medicine  as medicine2 
  2. from  A  a1 inner join A  a2  on a1.id = a2.id and a1.medicine != a2.medicine
output:
Expand|Select|Wrap|Line Numbers
  1. medicine1 medicine2
  2. 1       2
  3. 2       1
  4. 1       3
  5. 3       1
  6. 2       3
  7. 3       2
i don't know how to do for to obtain only the pairs differtent, because (1,2) and (2,1) are the same, some can help me,please
thanks in advance
Oct 29 '08 #1
3 2081
coolsti
310 100+
Expand|Select|Wrap|Line Numbers
  1. select a1.medicine as medicine1, a2.medicine as medicine2 
  2. from A a1 inner join A a2 on a1.id = a2.id and a1.medicine != a2.medicine and a1.medicine < a2.medicine
Won't the above work?
Oct 30 '08 #2
Atli
5,058 Expert 4TB
Please use [code] tags when posting your code examples. (See How to ask a question)

[code=sql] ...SQL query goes here... [/code]

Thank you.
Moderator
Oct 31 '08 #3
coolsti
310 100+
Actually, thinking about it, my suggestion might not work.

If not, you could try this:

Expand|Select|Wrap|Line Numbers
  1. select distinct 
  2. if(a1.medicine<a2.medicine,a1.medicine,a2.medicine) as medicine1, 
  3. if(a1.medicine<a2.medicine,a2.medicine,a1.medicine) as medicine2 
  4. from A a1 inner join A a2 on a1.id = a2.id 
  5. and a1.medicine != a2.medicine and a1.medicine < a2.medicine 
  6.  
Here I do the select as I suggested before, but I make sure that for each row selected, the two medicines that are taken as the two attributes in the resulting table are ordered so that medicine1 is always smaller than medicine2.

This would then transform the two pairs, e.g. "1 2" and "2 1" into two equal rows looking like "1 2".

Then I request that only "distinct" rows are returned. The distinct should then eliminate the duplicates that come about because of the ordering of the medicines.

.........................

Thinking about it some more (too lazy to test it), my original suggestion should work anyway. On the way to work today I thought of some reason why it would not, but now I cannot remember the reason :)
Oct 31 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Dave | last post by:
Hi all, I've been trying to figure this out for the last day and a half and it has me stumped. I've got a web application that I wrote that keeps track of trading cards I own, and I'm moving it...
2
by: Bruce Duncan | last post by:
I'm a bit new to MySQL (know MS SQL well...and that may be the problem...getting the syntax confused) and I'm having a join problem...can anyone offer some help? Here's my problem: I have table1...
1
by: Keith | last post by:
I have created a view to test some of the data in my database. I am relatively new to SQL so may have caused this problem by doing something wrong. I have a table called SYS_Individual which...
10
by: david | last post by:
Hi, I have the following code which returns an error when run as part of my ASP SQL.... strquery = strquery & "FROM (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID)...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
1
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
21
by: mollyf | last post by:
I'm creating a query, which I want to use in code in my VB.NET app. This query produces the correct results when executed in Access: SELECT tblEncounters.EncounterBeginDT, Query11.RID,...
1
by: imranpariyani | last post by:
Hi i have a severe performance problem with one of my views which has 6 to 8 joins .. any help will be appreciated.. the view is: CREATE OR REPLACE VIEW thsn.trade_view AS SELECT...
10
by: MLH | last post by:
Gentlemen: I am having one heck of a time taking a DAO walk through the records in an SQL dynaset. I'm trying to walk a set of records returned by a UNION query. I'm attempting to filter the...
3
by: Anila | last post by:
Hi Friends, My problem with Inner join is ... first i joined two tables and i got the result. after that iam trying to join one more table its giving syn tax error in JOIN condition. ...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.