以下查询有效,但 where 子句的“o.opendate 不为 null 且 o.orderdate 不为 null 且 o.closedate 为 null 且 o.canceldate 为 null”部分只需要应用于 orderID 计数 (orderCount)。现在它适用于整个结果集,这不是我想要的。如何更改查询以执行此操作?这些表也非常大,所以我非常依赖索引并且出于性能原因尽量不使用子查询。任何帮助,将不胜感激
select s.ZipCode, count(o.[OrderID]) orderCount, b.Longitude, b.Latitude, b.ordering
from ZipCodeServiceAvailability s
left join pdx_orders_view o on s.ZipCode = left(o.[ZipCode], 5)
left join ZipCodeBoundaries b on s.ZipCode = b.ZipCode
Where s.state = 'TX' and Ordering % 10 = 0 and
o.opendate is not null and o.orderdate is not null and o.closedate is null and o.canceldate is null
Group By s.ZipCode, IsServiced, b.Longitude, b.Latitude, b.Ordering
Order by s.ZipCode, b.Ordering