× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



BTW you can directly include the CASE Clause in the order by:

select {}
from {}
where state='FL'
or county='DADE'
or city='MIAMI'
or postal='33166'
order by case when zip=33166 then 1
when city = 'MIAMI' then 2
when county = 'DADE' then 3
when state = 'FL' then 4
else 9
end
fetch first row only

Mit freundlichen Grüßen / Best regards

Birgitta Hauser

"Shoot for the moon, even if you miss, you'll land among the stars." (Les Brown)
"If you think education is expensive, try ignorance." (Derek Bok)
"What is worse than training your staff and losing them? Not training them and keeping them!"
„Train people well enough so they can leave, treat them well enough so they don't want to.“ (Richard Branson)


-----Ursprüngliche Nachricht-----
Von: MIDRANGE-L [mailto:midrange-l-bounces@xxxxxxxxxxxxxxxxxx] Im Auftrag von Peter Dow
Gesendet: Thursday, 02.2 2023 22:47
An: midrange-l@xxxxxxxxxxxxxxxxxx
Betreff: Re: SQL SELECT: controlling the order of evaluation

You want to only return 1 record, with priority given to a match on zip, then city, etc.?

Maybe something like

select {}
, case when zip=33166 then 1
when city = 'MIAMI' then 2
when county = 'DADE' then 3
when state = 'FL' then 4
else 9
end Priority
from {}
where state='FL'
or county='DADE'
or city='MIAMI'
or postal='33166'
order by priority
fetch first row only


On 2/2/2023 12:25 PM, x y wrote:
When I have multiple conditions on my WHERE clause, can I control the
order in which they're evaluated?

Example: the client in postal code 33166, Miami, Dade County, Florida,
is looking for a local resource. I'd like to write select {} from {}
where state='FL' or county='DADE' or city='MIAMI' or postal='33166'
fetch first row only and then tell SQL to return a result by
evaluating first on postal, then city, then county, and then state.

I'm trying to avoid multiple SELECT statements. It's impractical to
add "helper" data in the table, which will have > 100 million records.
Putting "postal='33166'" first doesn't provide the desired result. A
UNION ALL won't work. I don't think a JOIN will work but I'm trying
to figure out how it might work.

It looks like SQL selects the first row that meets any of the
selection criteria, i.e., SQL is giving all the conditions equal
weight). I'm using CASE in my ORDER BY for other requirements but I
can't figure out if that's the solution here.

Thanks for your ideas!

--x
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list To post a message email: MIDRANGE-L@xxxxxxxxxxxxxxxxxx To subscribe, unsubscribe, or change list options,
visit: https://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives at https://archive.midrange.com/midrange-l.

Please contact support@xxxxxxxxxxxxxxxxxxxx for any subscription related questions.



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.