简体   繁体   中英

How to add date range filter to sub-query in SQL server?

I tried to create a report to get sales and collection data for a particular date range. But I received some data which are not belongs to that date period. How to solve this? Here my query

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

Highlighted row of following pic should only display 'fbsales amount' but it displays 'fbdwnp withoit vat' amount also.Because sales date is 30.12.2019 and collection date is 09.01.2020.

What can i do to overcome this issue

在此处输入图片说明

Try adding:

and a.DocDate is not null

to the query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM