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

To fetch common values between two rows

Suppose a table contains fields such as member_id,item1,item2 and item3.Then how can we fetch common items between two rows.

eg:- mem_id item1 item2 item3
1 a b c
2 b c d
3 d f g

common items between first 2 rows is 'b' and 'c'.
How can we write a query for this.

thanks in advance
Feb 22 '08 #1
5 2827
radcaesar
759 Expert 512MB
Suppose a table contains fields such as member_id,item1,item2 and item3.Then how can we fetch common items between two rows.

eg:- mem_id item1 item2 item3
1 a b c
2 b c d
3 d f g

common items between first 2 rows is 'b' and 'c'.
How can we write a query for this.

thanks in advance
What is your exact requirement ? Which two colums you want to compare if the table have 100 records ?
Feb 22 '08 #2
It's a very unusual requirement. Could we perhaps know what the table is for?

At this stage, my first reaction would be to see if the columns item1, item2, etc would be more appropriate as rows in a different table. Then the query becomes easier, and the database schema will meet your needs better.
Feb 23 '08 #3
It's a very unusual requirement. Could we perhaps know what the table is for?

At this stage, my first reaction would be to see if the columns item1, item2, etc would be more appropriate as rows in a different table. Then the query becomes easier, and the database schema will meet your needs better.
Thanku for your attention.
I'll explain with another eg.

If i have a 2 tables first with member_id,mem_name and location and another
with member_id, interest1,interest2 and interest3 as fields.
Suppose the tables contains 1000 rechords.
If a new member came , i want to compare that member's interests with all the members in the table and return the number of common interests in each rechord.
Feb 23 '08 #4
jagged
23
Brad is right, you really need to change your schema and store the interests as rows in a different table, ie

mem_id, interest
1, golf
1, tennis
2, polo
2, tennis

then it's easier to extract info afterwards or even to have more than 3 interests later.

If you really can't change it, then you can do something like

SELECT mem_id
FROM
(
SELECT mem_id, interest1 as [Interest] FROM table2
UNION SELECT mem_id, interest2 as [Interest] FROM table2
UNION SELECT mem_id, interest3 as [Interest] FROM table2
) tmp
WHERE interest = 'whatever'

but that's really not the right way to do things.
Feb 23 '08 #5
Yes, I agree with Jagged. You can use his script to simulate the answer you want, but you really, really should change your table structures if at all possible. Your efforts now will save you a lot of work in the future. Good luck.
Feb 24 '08 #6

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

Similar topics

8
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city ...
8
by: Evan Smith | last post by:
During a routine performance check using an event monitor, I discovered a class of query whose performance has me baffled. The monitor captured: SELECT * FROM EWM_CASE fetch first 1 rows only...
3
by: becoolmun | last post by:
It seems with V7 of DB2 I can now use Fetch First 1 Rows Only in a Select stmt, whic is great, because I don't have to use a cursor. Unfortunately, it doesn't allow ORDER BY in the same Select and...
7
by: Jeff | last post by:
Out of curiosity, I tried to check the help file for the property 'Fetch Defaults' to see what it is for. No surprise, it is not mentioned. F1 on the property give nothing. A search of MS knowledge...
4
by: db2admin | last post by:
hi, For the following SQL and plan ============================================================== SELECT Q1.PLCY_KY, Q1.PLCY_TRM_EFCTV_DT, Q1.ACNTG_BGN_DT, CASE WHEN ((Q1.PLCY_SRC_CD = '02')...
5
by: cdtsly | last post by:
Hi i have a table with all value at 4 i select all lines in a fetch i update one with a value of 7 i update all the row in the fetch with a value 5 the result is that all my row are at 5 and...
0
by: vinidimple | last post by:
Hi i have a serious problem while i was working in Excel.I want to fetch columns from an excel worksheet and i need to compare it with an sql querry fields,so i tried to open an excle...
0
by: LamSoft | last post by:
I cannot get values from detailview, here is the demo code... <Fields> <asp:BoundField DataField="serverip" HeaderText="Server IP" ReadOnly="true" SortExpression="serverip" /> <asp:BoundField...
3
by: nrain | last post by:
Hi This is nrain here in a pl/sql block, some rows are getting inserted into the table violating the primary key and going to exception. I need to fetch records and want to check what...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.