0
我有3個表ImportRecord,SiteOrder和Parcel其中ImportRecord.ID = SiteOrder.ImportId和SiteOrder.ID = Parcel.SiteOrderId。從子查詢獲得計數
我需要一個查詢檢索以下:
declare @Started as varchar(50) = null
declare @Ended as varchar(50) = null
declare @ClientCode as varchar(50) = null
declare @FileName as varchar(50) = null
declare @PageSize as int = null
select Count(so.ID) as TotalOrders, Count(p.ID) as TotalParcels,
--Count(so.Status <> 1 or so.Status <> 2) as TotalNotDespatched,
--Count(so.Status = 3 or so.Status = 8 or so.Status = 7) as TotalInError,
ir.ID, ir.Filename, ir.Started, ir.Status, ir.ClientCode
from ImportRecord ir with (nolock)
join SiteOrder so with (nolock)
on so.ImportId = ir.ID
join Parcel p with (nolock)
on p.SiteOrderId = so.ID
where 1=1
and ir.Status <> 5 --NOT DELETED
and (@ClientCode is null or ir.ClientCode = @ClientCode)
and (@Started is null or ir.Started = @Started)
and (@Ended is null or ir.Ended = @Ended)
and (@ClientCode is null or ir.ClientCode = @ClientCode)
and (@FileName is null or ir.Filename like '%' + @FileName + '%')
group by ir.ID, ir.Filename, ir.Started, ir.Status, ir.ClientCode
order by ir.ID desc
如何返回所有siteorders的計數,其中狀態<> 1或<> 2這是TotalNotDespatched並且其中狀態= 3, 7和8的TotalInError?
您正在使用哪種SQL實現? – WilliamD