Hi, I'm rather new to writing queries, so please forgive me if this is a really simple thing to do.
I need to create a query that finds out which rows in the DB have ANY of the values that are in a given list. Only one column in each row of the DB has their values stored in a list.
Example:
Passed list = 2,5,10,6
ColumX data in database:
Row 1 = 3, 5, 21, 4
Row 2 = 1, 4, 7, 9
Row 3 = 2
Row 4 = 8, 10, 3, 6
Row 5 = 1, 22, 9, 4
Base on the passed list, the query should return the following rows:
Row 1 (because 5 was in the list)
Row 3 (because 2 was in the list)
Row 4 (because 10 and 6 was in the list)
I’m hoping threes some SQL function that works with lists and can compare the data sets of two lists to find matching values, but I don’t know.
Can anyone help?
thanks
3 1767
Hi, I'm rather new to writing queries, so please forgive me if this is a really simple thing to do.
I need to create a query that finds out which rows in the DB have ANY of the values that are in a given list. Only one column in each row of the DB has their values stored in a list.
Example:
Passed list = 2,5,10,6
ColumX data in database:
Row 1 = 3, 5, 21, 4
Row 2 = 1, 4, 7, 9
Row 3 = 2
Row 4 = 8, 10, 3, 6
Row 5 = 1, 22, 9, 4
Base on the passed list, the query should return the following rows:
Row 1 (because 5 was in the list)
Row 3 (because 2 was in the list)
Row 4 (because 10 and 6 was in the list)
I’m hoping threes some SQL function that works with lists and can compare the data sets of two lists to find matching values, but I don’t know.
Can anyone help?
thanks
You might get stuck with having to use a cursor.
This should get you started. -
DECLARE @PassedList VARCHAR(5000)
-
DECLARE @ParsedList VARCHAR(5000)
-
DECLARE @ROW VARCHAR(5000)
-
set @passedlist = '2,5,10,6'
-
-
declare @ParsedInt int
-
declare @CompareInt int
-
-
-
-
DECLARE C cursor LOCAL FAST_FORWARD FOR
-
SELECT ColumnX
-
From TableTest
-
-
OPEN C
-
-
FETCH NEXT FROM C INTO @Row
-
-
WHILE @@Fetch_Status = 0
-
BEGIN
-
-
SET @ParsedList = @PassedList
-
while len(@ParsedList) > 0
-
BEGIN
-
IF charindex(',',@ParsedList) > 0
-
BEGIN
-
SET @ParsedInt = substring(@ParsedList,0,charindex(',',@ParsedList))
-
SET @ParsedList = substring(@passedlist,charindex(',',@ParsedList) + 1, len(@ParsedList))
-
END
-
ELSE
-
BEGIN
-
SET @ParsedInt = @ParsedList
-
SET @ParsedList = ''
-
END
-
-
WHILE LEN(@Row) > 0--compare every comma delimited value in row to the parsed int from the passed list
-
BEGIN
-
IF charindex(',',@Row) > 0
-
BEGIN
-
SET @CompareInt = substring(@Row,0,charindex(',',@Row))
-
SET @Row = substring(@Row,charindex(',',@Row) + 1, len(@Row))
-
END
-
ELSE
-
BEGIN
-
SET @CompareInt = @Row
-
SET @Row = ''
-
END
-
-
IF (@CompareInt = @ParsedInt)
-
BEGIN
-
--DO SOMETHING
-
print 'Found a match'
-
END
-
-
-
END--end row parse row loop
-
END--end parse passedlist loop
-
-
-
-
FETCH NEXT FROM C INTO @Row
-
-
END
-
-
Infide
Thanks for the reply, but dam, I've never seen SQL code like that. Looks more like a program in C than a query.
I copy and pasted what you wrote into my query (its being sent within a Cold Fusion query) and the system doesn't like it. No error, but the query name that was given in the CF tag doens't is not available for the Output.
Would there be a simpler way you think to compare the values of these to lists?
cheers
F.
You might get stuck with having to use a cursor.
This should get you started. -
DECLARE @PassedList VARCHAR(5000)
-
DECLARE @ParsedList VARCHAR(5000)
-
DECLARE @ROW VARCHAR(5000)
-
set @passedlist = '2,5,10,6'
-
-
declare @ParsedInt int
-
declare @CompareInt int
-
-
-
-
DECLARE C cursor LOCAL FAST_FORWARD FOR
-
SELECT ColumnX
-
From TableTest
-
-
OPEN C
-
-
FETCH NEXT FROM C INTO @Row
-
-
WHILE @@Fetch_Status = 0
-
BEGIN
-
-
SET @ParsedList = @PassedList
-
while len(@ParsedList) > 0
-
BEGIN
-
IF charindex(',',@ParsedList) > 0
-
BEGIN
-
SET @ParsedInt = substring(@ParsedList,0,charindex(',',@ParsedList))
-
SET @ParsedList = substring(@passedlist,charindex(',',@ParsedList) + 1, len(@ParsedList))
-
END
-
ELSE
-
BEGIN
-
SET @ParsedInt = @ParsedList
-
SET @ParsedList = ''
-
END
-
-
WHILE LEN(@Row) > 0--compare every comma delimited value in row to the parsed int from the passed list
-
BEGIN
-
IF charindex(',',@Row) > 0
-
BEGIN
-
SET @CompareInt = substring(@Row,0,charindex(',',@Row))
-
SET @Row = substring(@Row,charindex(',',@Row) + 1, len(@Row))
-
END
-
ELSE
-
BEGIN
-
SET @CompareInt = @Row
-
SET @Row = ''
-
END
-
-
IF (@CompareInt = @ParsedInt)
-
BEGIN
-
--DO SOMETHING
-
print 'Found a match'
-
END
-
-
-
END--end row parse row loop
-
END--end parse passedlist loop
-
-
-
-
FETCH NEXT FROM C INTO @Row
-
-
END
-
-
Infide
Thanks for the reply, but dam, I've never seen SQL code like that. Looks more like a program in C than a query.
I copy and pasted what you wrote into my query (its being sent within a Cold Fusion query) and the system doesn't like it. No error, but the query name that was given in the CF tag doens't is not available for the Output.
Would there be a simpler way you think to compare the values of these to lists?
cheers
F.
Refer this http://weblogs.sqlteam.com/dinakar/a.../28/60150.aspx Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Funnyweb |
last post by:
I have a database table, which has field that could contain a single integer
or a list of comma separated integers.
Is it possible to match each row of that field against an array of integers
and return those rows where any of the integers in that field are in my
array?
For example suppose I did the following:
arInts = array(2,4,6,8,10);
|
by: waters |
last post by:
I seem to have hit a snag.
I am trying to add the list (l_local) as an item in the output list
(l_output). "l_local" is a modified copy of the format-list "l_format",
which will be updated by index position within the function. The problem
occurs in the following statement block:
def create_apid_list(l_format,l_values):
l_output =
for t_curr_line in l_values: # ...for each list in the values list
|
by: Paul Mateer |
last post by:
Hi,
I have been running some queries against a table in a my database and
have noted an odd (at least it seems odd to me) performance issue.
The table has approximately 5 million rows and includes the following
columns:
DocID (INTEGER, PRIMARY KEY, CLUSTERED)
IsRecord (INTEGER, NONCLUSTERED)
|
by: jjturon |
last post by:
Can anyone help me??
I am trying to pass a Select Query variable to a table using Dlookup
and return the value to same select query but to another field.
Ex.
SalesManID SalesManName
AT Alan Time
|
by: skumar434 |
last post by:
i need to store the data from a data base in to structure
.............the problem is like this ....suppose there is a data base
which stores the sequence no and item type etc ...but i need only the
sequence nos and it should be such that i can access it through the
structure .plz help me .
| |
by: Hemant Shah |
last post by:
Folks,
I am having problem with an application that uses static SQL,
the application basically browses through the table given start and
end key most of the time it is processed from begining to end.
The db2expln tells me that there is no Data or Index prefetch.
I am running DB2 UDB 8 on AIX 5.3, and I am using DMS tablespace on
raw logical volumes on a SAN.
|
by: trint |
last post by:
Hi,
I have a dataGrid that when I try to add a row, I can't keep the data
separated into columns.
Here is what I tried to separate the columns (a coma):
string completeProductsString = new string;
DataGridViewRowCollection rows = this.dataGridView1.Rows;
completeProductsString = productsString0 + "," +
productsString1;
rows.Add(completeProductsString);
|
by: Frank11 |
last post by:
Hi, I'm rather new to writing queries, so please forgive me if this is a really simple thing to do.
I need to create a CF query that finds out which rows in the DB have ANY of the values that are in a given list. Only one column in each row of the DB has their values stored in a list.
Example:
Passed list = 2,5,10,6
ColumX data in database:
Row 1 = 3, 5, 21, 4
|
by: vikjohn |
last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified CGI application misbehaved by not returning a complete set of HTTP headers. The scripts are very long but here are the opening statements:
The One that works ....
#!C:\Perl\bin\perl.exe
# openresolver.cgi
#
# OpenResolver - a CGI script for...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |