How to add an exception into a SQL Select statement?

74 Views Asked by At

I have a company table where company_Code is the highest level of the heirarchy, and under each compnay_Code there are numerous divisions. I have a select statement where I have several company codes that should not be included, but I need to make an exception within one of those company codes to still include 2 of the divisions. There are 904 divisions total for this company_code. The current statement is:

select top (1000000) companyid 
,companyfullname
,company_code
,division
,reportingname
,division_name
,country 
,accountname
from totaldw.dbo.companymap
where company_code not in ('1760','1785','1771','1831','1740')

but I need to modify so that company_Code 1771 is still not included unless the division in ('01188', '01189').

1

There are 1 best solutions below

0
courty340 On
select top (1000000) companyid 
,companyfullname
,company_code
,division
,reportingname
,division_name
,country 
,accountname
from totaldw.dbo.companymap
where (division in ('01188','01189') and company_code in ('1771')) or company_code not in ('1760','1785','1771','1831','1740')