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
5 2827
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 ?
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.
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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 ...
|
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...
|
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...
|
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...
|
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')...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
| |