Creating Search Queries using indents

  • Thread starter Thread starter DaveC426913
  • Start date Start date
  • Tags Tags
    Search
Click For Summary
SUMMARY

The forum discussion centers on constructing search queries in a proprietary database while addressing issues related to logical operators and indentation. Users encountered warnings about using "and" and "or" at the same indent level, which led to confusion regarding query structure. The solution involves ensuring proper indentation and bracketing of conditions, particularly for age and date ranges, to avoid false positives in results. Ultimately, the correct query format was established, emphasizing the importance of logical operator precedence and clarity in query construction.

PREREQUISITES
  • Understanding of logical operators (AND, OR) in query construction
  • Familiarity with indentation and bracketing in programming syntax
  • Knowledge of date range filtering techniques
  • Experience with proprietary database query languages
NEXT STEPS
  • Learn about logical operator precedence in query languages
  • Explore best practices for structuring complex queries in proprietary databases
  • Investigate debugging techniques for query results to minimize false positives
  • Study advanced filtering methods using date ranges in database queries
USEFUL FOR

Database developers, data analysts, and anyone involved in constructing complex search queries within proprietary database systems will benefit from this discussion.

DaveC426913
Gold Member
2025 Award
Messages
24,249
Reaction score
8,367
TL;DR
Why is this search query warning me about and/or indentation?
I'm constructing a search query in a proprietary database and I'm getting a warning about my use of ands and ors.

I've included the search as pseudocode. I have inserted square brackets whereas the actual query only uses indents (which I cannot get my head around).

What I am expecting to get is:
  • Enrolled members
  • between ages 50 and 74 (incl.)
  • who have had at least one of the following (during the specified 30 month window):
    • Q code along with SOB or FOBT
    • FIT
    • SIG

Here is my concern:
When I test this the system responds with
"You should not have an and and an or at the same indent level."

Is my search not well-formed?
Can I rectify the problem simply by indenting the entire section after the third and that contains all the ors? (That would require an initial double-indentation, i.e. and [[ )
Code:
Member Status = Enrolled
and
Age >= 50
and
Age <= 74
and
[
    Q code date is earlier than Apr 1, 2021
    and
    Q code date is later than Sep 30, 2018
    and
    [
        [
            SOB date is earlier than Apr 1, 2021
            and
            SOB date is later than Sep 30, 2018
        ]
        or
        [
            FOBT date is earlier than Apr 1, 2021
            and
            FOBT date is later than Sep 30, 2018
        ]
    ]
]
or
[
    FIT date is earlier than Apr 1, 2021
    and
    FIT date is later than Sep 30, 2018
]
or
[
    SIG date earlier than Apr 1, 2021
    and
    SIG date is later than Sep 30, 2018
]
 
Technology news on Phys.org
You need more bracketing : the proggy is reading it as a and b and c and d or e or f or g (or something like that). [edit: *less* bracketing... let me see what I can come up with]

Apart from the obvious ranging (20<age<120 sort of thing) I can't really tell, either.

[edit: I suppose reading the whole of your post would help]

There is actually an order of precedence with logical operators, but nobody remembers what it is (thus the blanket "you can't do that" message).

You have seven conditions : MS Age Q SOB FOBT FIT SIG
Can you set them up with ands and ors and brackets, and then work on the details of each condition. [edit: or read the next post]
 
Last edited:
MS & <Age> & [ ( <Q> & { <SOB> | <FOBT> } ) | <FIT> | <SIG> ]
Something to that effect. As you've probably surmised & | is and and or, and <>[]{}() are all in/outdents and []brackets. I'd suggest an indentation for the two age conditions for legibility.
 
Last edited:
I would go the hacking route and try subsets of these queries taking out one condition term at a time until it works and then begin adding them back working from the outer level to the inner level.
 
  • Like
Likes   Reactions: sysprog
Thanks. I experimented with the editor and found out that it does, in fact, allow double indentations, so I can "simply" add more indents where- and as-needed.
Code:
Member Status = Enrolled
and
Age >= 50
and
Age <= 74
and
[
    [
        Q142 code date is earlier than Apr 1, 2021
        and
        Q142 code date is later than Sep 30, 2018
        and
        [
            [
                SOB date is earlier than Apr 1, 2021
                and
                SOB date is later than Sep 30, 2018
            ]
        or
            [
                FOBT date is earlier than Apr 1, 2021
                and
                FOBT date is later than Sep 30, 2018
            ]
        ]
    ]
    or
    [
        FIT date is earlier than Apr 1, 2021
        and
        FIT date is later than Sep 30, 2018
    ]
    or
    [
        SIG date is earlier than Apr 1, 2021
        and
        SIG date is later than Sep 30, 2018
    ]
]
Unfortunately, my results contain some "false positives".
It seems to be returning some SOB/FOBTs without meeting the Q142 requirement.


Working just fine now. The Q142 condition was not actually spelled correctly.

Thanks all!
 
Last edited:
You appear to be just tossing brackets randomly. Each of those "ranging" condition-pairs should be enclosed in their own brackets.
 
Last edited:
Code:
Query :
    MS = enrolled
AND
    (Age range)
AND (  (   (Q142 range)
       AND (  (SOB range)
           OR (FOBT range)
           )
       )
    OR (FIT range)
    OR (SIG range)
    )
 
Last edited:
hmmm27 said:
You appear to be just tossing brackets randomly.
Hah. No. Not sure why you'd say that since your solution actually has two more sets (9) than mine does (7). :wink:

And mine are virtually the same as yours, I just have them on separate lines because I didn't abbreviate the date conditional pairs like you did.

hmmm27 said:
Each of those "ranging" condition-pairs should be enclosed in their own brackets.
They all are, except the Q142. And it doesn't need its own set since it's part of a list of ANDs.

In yours, the age conditional pair doesn't need brackets.
 
Last edited:
DaveC426913 said:
Hah. No. Not sure why you'd say that since your solution actually has two more sets (9) than mine does (7). :wink:
Yeah, but mine's prettier.

And mine are virtually the same as yours, I just have them on separate lines because I didn't abbreviate the date conditional pairs like you did.
Exactly the same, but no way was I going to write that stuff out 20 times.
They all are, except the Q142. And it doesn't need its own set since it's part of a list of ANDs.

In yours, the age conditional pair doesn't need brackets.
Makes it easier to read : compiler will just toss them, anyways.
Code:
Query :
    Member Status = enrolled
AND
    (Age >= 50 AND
     Age <= 74)
AND (  (   (Q142 date is   later than Sep 30, 2018 AND
            Q142 date is earlier than Apr  1, 2021)
       AND (  (SOB  date is   later than Sep 30, 2018 AND
               SOB  date is earlier than Apr  1, 2021)
           OR (FOBT date is   later than Sep 30, 2018 AND
               FOBT date is earlier than Apr  1, 2021)
           )
       )
    OR (FIT date is   later than Sep 30, 2018 AND
        FIT date is earlier than Apr 1,  2021)
    OR (SIG date is   later than Sep 30, 2018 AND
        SIG date is earlier than Apr 1,  2021)
    )
Still prettier.
:wink:
 
Last edited:
  • Like
Likes   Reactions: DaveC426913

Similar threads

  • · Replies 15 ·
Replies
15
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
10
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K