question

CarlTevez avatar image
CarlTevez asked

problem with where or and query

I am trying to extract certain claims out of a table that fit specific criteria (there are around 36,000 cases in the database in total).

I want to extract all cases where an 'intervention' has either been commenced, or completed within the last 90 days and where the 'intervention type' = 2. I have used the following which seems to work, except for returning only intervention types equaling 2;


select claim_id, iv_commence_date, iv_completed_date, iv_reason
from hbintervention
where iv_commence_date> date ('today') -90 Or iv_completed_date > date('today') -90 and iv_reason = 2

I have also tried

select claim_id, iv_commence_date, iv_completed_date, iv_reason
from hbintervention
where iv_reason = 2 and iv_commence_date> date ('today') -90 Or iv_completed_date > date('today') -90


If I take the "or" section out to only return intervention type 2, then it does only return intevention type 2. However, as soon as I put it with the other conditions as above it only return the intervention types completed or commenced within 90 days.


any ideas would be much appreciated.


Thanks :)

sqlquery
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
anthony.green avatar image
anthony.green answered
select claim_id, iv_commence_date, iv_completed_date, iv_reason
from hbintervention
where iv_reason = 2 and (iv_commence_date> date ('today') -90 Or iv_completed_date > date('today') -90)

Try that, sometimes you need to separate the clauses using brackets in a where clause to make the independent of each other.

From what I have understood the questions to be.

Show me
IV_COMMENCE_DATE > TODAY-90 and IV_REASON = 2

And also show me
IV_COMPLETED_DATE > TODAY-90 and IV_REASON = 2

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

CarlTevez avatar image CarlTevez commented ·

Legend! - worked a treat that, thanks for your help :)

1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.