如何在SQL Server中向子查询添加日期范围过滤器?

问题描述 投票:0回答:1

我试图创建报告以获取特定日期范围内的销售和收款数据。但是我收到了一些不属于该日期段的数据。如何解决呢?这是我的查询

select a.DocDate as 'Order Date',f.DocDate as 'Collection Date',b.LineTotal as 'fbsales amount',a.SlpCode,a.CardCode,b.DocEntry,
sum(case when e.TaxCode = 'NBT2' then cast(e.LineTotal as decimal(22,8)) /1.02 
when e.TaxCode = 'N2+V15' then (cast(e.LineTotal as decimal(22,8)) /1.173)  
when e.TaxCode = 'VAT8' then (cast(e.LineTotal as decimal(22,8)) /1.08) else e.LineTotal end  ) as 'fbdwnp without vat',b.LineNum
from ORDR a
left join RDR1 b on b.DocEntry = a.DocEntry
left join OSLP c on c.SlpCode = a.SlpCode
left join OITM d on d.ItemCode = b.ItemCode
left Join (select TaxCode,LineTotal,BaseEntry,BaseLine,DocEntry from DPI1 where [TargetType]<>'14') e on e.BaseEntry = b.DocEntry and e.BaseLine = b.LineNum
left Join (select DocEntry,DocDate From ODPI where CANCELED = 'N' and DocDate between '20190101' and '20191231') f on f.DocEntry = e.DocEntry
where a.CANCELED = 'N' and d.U_Product_Type = 'Facebook'  and a.DocDate between '20190101' and '20191231' and a.SlpCode = 68
group by c.SlpName,a.SlpCode,b.LineTotal,d.DocEntry,a.CardCode,c.SlpName,b.DocEntry,a.DocDate,b.LineNum,f.DocDate

以下图片的突出显示的行应仅显示'fbsales金额',但也显示'fbdwnp含增值税'的金额。因为销售日期是2019年12月30日,收集日期是2020年1月9日。

我该怎么解决这个问题

enter image description here

sql sql-server-2012 subquery date-range
1个回答
0
投票

尝试添加:

and a.DocDate is not null

至查询

© www.soinside.com 2019 - 2024. All rights reserved.