Creating Search Queries using indents

  • Thread starter Thread starter DaveC426913
  • Start date Start date
  • Tags Tags
    Search
AI Thread Summary
The discussion centers on constructing a search query in a proprietary database, specifically addressing issues with the logical operators "AND" and "OR" at the same indent level. The user is trying to retrieve data on enrolled members aged 50 to 74 who have had specific medical codes within a defined time frame. They encounter a warning indicating that the query structure is not well-formed due to the improper use of "AND" and "OR" operators. Participants suggest that the user needs to adjust the indentation and bracketing of conditions to clarify the logical structure. They recommend enclosing each condition pair in brackets to ensure the query is correctly interpreted by the system. The conversation highlights the importance of logical operator precedence and the need for clear organization in query construction to avoid "false positives" in results. Ultimately, the user resolves their issue by correcting the spelling of a code and refining the query structure based on the feedback received.
DaveC426913
Gold Member
Messages
23,838
Reaction score
7,833
TL;DR Summary
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 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 DaveC426913
Back
Top