sql server - Sql using OR condation -


hi lets assume have below sample data

service_id  service_rno cust_name   agrement_id cust_id service_date    next_service 4                004    customer1   35           16     2016-01-03      2016-02-02 5                005    customer1   35           16     2016-02-03      2016-03-01 

and if have below query check if next_service not in service_date

select  dbo.service.service_id,          dbo.service.service_rno,          dbo.customer.cust_name,         dbo.service.agrement_id,         dbo.customer.cust_id,         dbo.service.service_date,         dbo.service.next_service  dbo.service  inner join dbo.customer on dbo.service.cust_id = dbo.customer.cust_id   next_service between '2016-02-01' , '2016-02-06'  ,    next_service not in (           select service_date            service s           s.cust_id = dbo.customer.cust_id )  order next_service 

output of above query second record

note service_date values in cases entered same next_service value in previous record customer

but have cases service_date entered after 1 or 2 days

so tried use blow query solve issue have

select  dbo.service.service_id,          dbo.service.service_rno,          dbo.customer.cust_name,         dbo.service.agrement_id,         dbo.customer.cust_id,         dbo.service.service_date,         dbo.service.next_service  dbo.service  inner join dbo.customer on dbo.service.cust_id = dbo.customer.cust_id   next_service between '2016-02-01' , '2016-02-06'  ,    next_service not in (           select service_date           service s           s.cust_id = dbo.customer.cust_id )  or    next_service not in (           select dateadd(d, -1, service_date)            service s           s.cust_id = dbo.customer.cust_id )  order next_service 

output of above query should empty, output 2 records issue?

the problem in condition. first row won't satisfy first part of condition, is

next_service between '2016-02-01' , '2016-02-06'  ,    next_service not in (           select dateadd(d, 1, service_date)            service s           s.cust_id = dbo.customer.cust_id )  

but passes second part

or    next_service not in (           select dateadd(d, 1, service_date)            service s           s.cust_id = dbo.customer.cust_id )  

what want is

where  next_service between '2016-02-01' , '2016-02-06'  , (next_service not in (           select s.service_date           service s           s.cust_id = dbo.customer.cust_id)       or next_service not in (           select dateadd(d, -1, s.service_date)            service s           s.cust_id = dbo.customer.cust_id) ) 

(notice parenthesis)

or differently:

where  next_service between '2016-02-01' , '2016-02-06'  , not exists (           select 1           service s           s.cust_id = dbo.customer.cust_id             , next_service between dateadd(d, -1, s.service_date) , s.service_date) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -