SQL (Multiple If / Not Like) Statements

rhjake23

New Member
Joined
Dec 20, 2017
Messages
35
Hello.
I have the following code. Which I can't get it to work.
All of the code works until I get to the OR statements.
Not sure if I need a case statement or ?
Any input would be much appreciated !
Thanks.

select a.agree_no as agreement_no,
a.um_agree_no as umbrella,
a.agree_class as classification,
a.agree_desc as description,
a.end_dat as enddate,
b.deleted as deleted1
from p_pricing.agreement_renewal_v1 b, p_master.agreement_v1 a
where a.agree_no = b.agree_no
and a.end_dat > '20200624'
and b.deleted is NULL
and a.agree_type IN ('ZDRB', 'ZCRB', 'ZDTI', 'ZCTI', 'ZCTD', 'ZDTD', 'ZDRC')
and a.sales_org_hdr <> '1002'
and a.sales_org_hdr <> '2002'
and a.agree_class ='ZG' and a.agree_desc not like 'GA-%' and a.end_dat > '20200624'
or a.agree_class ='ZC' and a.agree_desc not like 'CA-%' and a.end_dat > '20200624'
or a.agree_class = 'ZP' and a.agree_desc not like 'PA-%' and a.end_dat > '20200624'
or a.agree_class = 'ZMCA' and a.agree_desc not like 'MCA-%' and a.end_dat > '20200624'
group by a.agree_no, a.um_agree_no, a.agree_class, a.agree_desc, a.end_dat, b.deleted
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I don't have a way to test this right now, but this appears to be a case where adding some parentheses might help:

select a.agree_no as agreement_no,
a.um_agree_no as umbrella,
a.agree_class as classification,
a.agree_desc as description,
a.end_dat as enddate,
b.deleted as deleted1
from p_pricing.agreement_renewal_v1 b, p_master.agreement_v1 a
where a.agree_no = b.agree_no
and a.end_dat > '20200624'
and b.deleted is NULL
and a.agree_type IN ('ZDRB', 'ZCRB', 'ZDTI', 'ZCTI', 'ZCTD', 'ZDTD', 'ZDRC')
and a.sales_org_hdr <> '1002'
and a.sales_org_hdr <> '2002'
and ((a.agree_class ='ZG' and a.agree_desc not like 'GA-%' and a.end_dat > '20200624')
or (a.agree_class ='ZC' and a.agree_desc not like 'CA-%' and a.end_dat > '20200624')
or (a.agree_class = 'ZP' and a.agree_desc not like 'PA-%' and a.end_dat > '20200624')
or (a.agree_class = 'ZMCA' and a.agree_desc not like 'MCA-%' and a.end_dat > '20200624'))
group by a.agree_no, a.um_agree_no, a.agree_class, a.agree_desc, a.end_dat, b.deleted
 
Upvote 0

Forum statistics

Threads
1,223,646
Messages
6,173,536
Members
452,520
Latest member
Pingaware

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top