SQL query problems - please help

Quote
Uros Podlogar wrote:

> In my application I put queries together during execution.  I have some
> problems with WHERE sentence. Here is a sample of the SQL query:

> SELECT *
> FROM merit
> WHERE Datum >= :var0
> AND Datum <= :var1
> AND ( Ch1Max >= :var2
> OR Ch2Max >= :var3
> OR Ch3Max >= :var4
> OR Ch4Max >= :var5
> OR Ch1Min <= :var6
> OR Ch2Min <= :var7
> OR Ch3Min <= :var8
> OR Ch4Min <= :var9 )

> Here are the values:

> :var0 = 1. 11. 1996
> :var1 = 7. 11. 1996
> :var2 = 10.50
> :var3 =  8.40
> :var4 = 10.50
> :var5 = 11.20
> :var6 =  1.96
> :var7 =  1.96
> :var8 =  1.96
> :var9 =  1.40

> When I run query with date part (3. and 4. line) it works OK. It works
> OK with the second part too (lines 5. to 12.). The problems begin when I
> put those lines together. The problem is that in the result of this
> query are records dated 10. 10. 1996 which is not possible (because of
> the third and fourth line). I suspect that this SQL does not have the
> same precedence rules that ordinary Pascal (or any other language) or
> that I can not use ( ) in SQL quires. I searched trough the manuals, but
> there are only short samples and nothing more.

> I would like to know how can I combine AND and OR in SQL queries
> (obvious this is not working as I tried). If this is not possible, can I
> combine two queries. First for the date and the second for the limits
> (and use result from the first one as a input for the second one). How
> can this be done?

> Thanks for your help

> Uros

Try this:

SELECT *
FROM merit
WHERE (Datum >= :var0 AND Datum <= :var1) AND
      ((Ch1Max >= :var2) OR
       (Ch2Max >= :var3) OR
       (Ch3Max >= :var4) OR
       (Ch4Max >= :var5) OR
       (Ch1Min <= :var6) OR
       (Ch2Min <= :var7) OR
       (Ch3Min <= :var8) OR
       (Ch4Min <= :var9) )

I THINK this is a syntactical problem. You gotta be VERY explicit when
combining AND/OR booleans in SQL or any Pascal code. I have chased bugs
for days before realizing that a seemingly straighforward piece of
boolean logic will fail if the parentheses are not structured correctly,
especially when combining AND with OR.

HTH
--
Howard Chandler, Jr.
chan...@iglou.com