如何计算q.atindt列和q.orderdate列之间的天数?因为在某些情况下我没有得到正确的数字

我试图得到两个日期q.atindt-q.orderdate之间的差值,但是在某些情况下,我得到的数字像是被乘以两次或三次,有时看起来它在计算中没有模式,我尝试了max函数,但这没用....

concat(q.CHSeries,q.CHNumber) as "Chassi no.",q.CHNumber as "Chassi no.2",q.CHNumber *1 as "Chassi no.1",q.Model,q.ORDER,q.atindt as "Date of Invoice",q.atinvn as "Invoice no",q.customer as "Customer",q.orderdate as "Orderdate",(select coalesce(sum(lyamnt*lyICVL),0) from thf311 where q.order=lyorno)+
(select coalesce(sum(nmamnt*nmICVL),0) from thf351 where  q.order=nmorno) as "Workshop Sales",(select coalesce(sum(lxamnt*lxICVL),0) from thf310 where  q.order=lxorno) +
(select coalesce(sum(nlamnt*nlICVL),0) from thf350 where  q.order=nlorno)as "Part Sales",0) from thf311 where  q.order=lyorno) + (select coalesce(sum(lxamnt*lxICVL),0) from thf310 where q.order=lxorno)+
(select coalesce(sum(nmamnt*nmICVL),0) from thf351 where  q.order=nmorno) + (select coalesce(sum(nlamnt*nlICVL),0) from thf350 where  q.order=nlorno)
 as "Total Sales",q.atindt-q.orderdate 
FROM 
(select ATVIN2,ATVIN3,ATPRDT,ATGNN2,aTorno,nlpcno,atindt,atinvn,atca30,atordt FROM THF020 h,THF350 WHERE nlBRNN=h.AtGNN2 AND nlORNO=h.AtORNO union select AtVIN2,AtVIN3,AtPRDT,AtGNN2,atorno,nmpcno,atordt 
FROM THF020 h,THF351 WHERE nmBRNN=h.AtGNN2 AND nmORNO=h.AtORNO
union
select AjviN2,AjviN3,AJPRDT,AJGNN2,ajorno,lxpcno,ajindt,ajinvn,ajca30,ajordt FROM THF010 h,THF310 WHERE lxBRNN=h.AjGNN2 AND lxORNO=h.AjORNO union select AjviN2,AjviN3,AjPRDT,AjGNN2,lypcno,ajordt
FROM THF010 h,THF311 WHERE lyBRNN=h.AjGNN2 AND lyORNO=h.AjORNO) as q(CHSeries,CHNumber,Model,BRANCH,ORDER,PAYCDE,customer,orderdate)
where atindt>='2019-01-01' and atindt<='2019-10-25' and paycde>39 and paycde <60  and concat(q.CHSeries,q.CHNumber) in 
(chassi 1,chassi2) 
group by
concat(q.CHSeries,q.CHNumber),q.CHNumber,q.branch,q.atindt,q.atinvn,q.customer,q.orderdate```
fghjq 回答:如何计算q.atindt列和q.orderdate列之间的天数?因为在某些情况下我没有得到正确的数字

您需要致电DAYS

DAYS(q.atindt)-DAYS(q.orderdate)

原因是日期差的算术方法并不总是等于天,请参见:

  

如果DAY(DATE2)      

如果DAY(DATE2)> DAY(DATE1),则DAY(RESULT)= N + DAY(DATE1)-   DAY(DATE2),其中N = MONTH(DATE2)的最后一天。 MONTH(DATE2)是   然后加1。

     

如果MONTH(DATE2)      

如果MONTH(DATE2)> MONTH(DATE1),则MONTH(RESULT)= 12 + MONTH(DATE1)   -MONTH(DATE2)和YEAR(DATE2)增加1。

     

YEAR(RESULT)= YEAR(DATE1)-YEAR(DATE2)

在:https://www.ibm.com/support/knowledgecenter/en/SSEPEK_10.0.0/sqlref/src/tpc/db2z_datearithmetic.html

本文链接:https://www.f2er.com/3096687.html

大家都在问