寻找没有为某些服务收费的订单

问题描述 投票:-2回答:1

我正在尝试查询我们的数据库,以查找提供某些服务的所有工作,但是订单中的那些产品没有收费。我尝试过多种方式,但我无法弄明白。任何帮助,将不胜感激。这基本上就是我要找的东西:

declare @startDate datetime = '1/01/2018';
declare @endDate datetime = '2/28/2018';

select o.orderid
from orders as o
left join orderitem as oi on oi.OrderID = o.OrderID
left join job as j on j.JobID = o.JobID
left join jobservice as js on js.JobID = j.JobID
where oi.ProductID not in ('55', '65', '78')
and js.ServiceID in ('13', '16')
and o.ShippedToClientID = j.ClientID
and j.JobDate between @startDate and @endDate + 1
and o.IsVoided = '0'
group by o.orderid
sql where-clause
1个回答
0
投票

我会用not exists。像这样的东西:

select o.orderid
from orders o
where not exists (select 1
                  from orderitem oi join 
                       job j
                       on j.JobID = o.JobID join
                       jobservice js
                       on js.JobID = j.JobID
                   where oi.OrderID = o.OrderID and
                         o.ShippedToClientID = j.ClientID and
                         oi.ProductID not in (55, 65, 78) and
                         js.ServiceID in (13, 16) and
                         j.JobDate between @startDate and @endDate + 1 and
                         o.IsVoided = 0
                  );
© www.soinside.com 2019 - 2024. All rights reserved.