473,399 Members | 2,774 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,399 software developers and data experts.

Where clause - knowing what part results matched on - possible?

Hi,

If you run a query which has a WHERE statement in which has a few
possibilities separated OR statements, e.g.

Select * from table where (Afield = 2) OR (Bfield = 2) OR (Cfield = 2)

In the returned results is it possible to know what particular part of the
WHERE clause a field matched.

For example, if the above query returned 6 rows, I would want to be able to
tell know if the first result matched on Afield and Cfield but not Bfield.
Then see that row 2 matched on Bfield only etc.

Is this possible?

Thanks in advance.

David

Apr 16 '06 #1
2 1481
>If you run a query which has a WHERE statement in which has a few
possibilities separated OR statements, e.g.

Select * from table where (Afield = 2) OR (Bfield = 2) OR (Cfield = 2)

In the returned results is it possible to know what particular part of the
WHERE clause a field matched.
Yes. Look at the results returned and evaluate the conditions of
the WHERE clause in a programming language.
For example, if the above query returned 6 rows, I would want to be able to
tell know if the first result matched on Afield and Cfield but not Bfield.
Then see that row 2 matched on Bfield only etc.


You can also select additional data to be returned with the record
using an arbitrary expression, like:

if(afield = 2, 1, 0) as matchedona,
if(bfield = 2, 1, 0) as matchedonb,
if(cfield = 2, 1, 0) as matchedonc

Gordon L. Burditt
Apr 16 '06 #2

Now for the ANSI standard version of the query.

select Afield,Bfield,Cfield, -- get the values of these fields
case when Afield=2 then 1 else 0 end as matchedonA,
case when Bfield=2 then 1 else 0 end as matchedonB,
case when Cfield=2 then 1 else 0 end as matchedonC,
other,
fields
from .....
where.....

1 and 0 can be 'A' and NULL or 'A' and 'Z' or. or or.....

It is a bit more wordy, but will work on MySQL, Oracle RDBMS(8,9,10),
Oracle Rdb, DB2, Informix, Sybase, and yes, even SQLServer (I think???)

Apr 17 '06 #3

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

Similar topics

6
by: Kevin Frey | last post by:
Hello, I have a table which contains some nullable columns. I want to write a single query, which can be prepared (ie. prepared statement), that can handle null or non-null values for the where...
3
by: A.V.C. | last post by:
Hello, I found members of this group very helpful for my last queries. Have one problem with CASE. I can use the column name alias in Order By Clause but unable to use it in WHERE CLAUSE. PLS...
4
by: Tom Walker | last post by:
I cannot get the WHERE statement to work correctly unless I use a literal with the LIKE. I want to use a working storage data name so that I can vary the WHERE statement. Example that works: ...
27
by: Chris, Master of All Things Insignificant | last post by:
I have come to greatly respect both Herfried & Cor's reponses and since the two conflicted, I wanted to get some clarification. My orginal post: Herfried, maybe your example here can get you to...
6
by: Index | last post by:
Hi, I have a file which I want to sort depending on multiple columns. Actually I want to implement the following query through programming: SELECT SUM(VALUES) GROUP BY x,y ORDER BY x,y; the...
2
by: Jim.Mueksch | last post by:
I am having a problem with using calculated values in a WHERE clause. My query is below. DB2 gives me this error message: Error: SQL0206N "APPRAISAL_LESS_PRICE" is not valid in the context where...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
8
by: chrisdavis | last post by:
I'm trying to filter by query or put those values in a distinct query in a where clause in some sort of list that it goes through but NOT at the same time. Example: ROW1 ROW2 ROW3 ROW4 ,...
9
by: Yitzak | last post by:
Hi spent a few hours on this one wrote a query that joined on results of 2 other queries. Qry3 using Qry1 and Qry2 When I used Qry1.FasText <cstr(Qry2.FasInteger) in the where clause - got...
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:
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
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
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
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,...
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.