0

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').

jarlh
  • 42,561
  • 8
  • 45
  • 63
courty340
  • 101
  • 3
  • 16
  • If it's just a single exception - then remove `1771` from the list and simply add it into your code with `OR`. – PM 77-1 Jan 23 '23 at 17:10
  • 2
    Need to sprinkle some parens and `and`/`or` operators there; `where (division_code in ('01188','01189') and company_code in ('1771')) or company_code not in (...)` – Mathieu Guindon Jan 23 '23 at 17:12

1 Answers1

1
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')
courty340
  • 101
  • 3
  • 16