具有DateAdd功能的DatePart(Quarter)

时间:2017-06-14 16:38:38

标签: sql sql-server-2012

我正在查询日期并将四分之一拉出来。我有两个相似的查询。一个是工作而另一个不工作。

两个查询都需要退出季度并将其列为QEDD - 我试图在最终报告中比较这些季度的展示季度和年份。

工作查询:

SELECT Sum(lm.Qty) as Proposed, lm.Entity as Entity, List = 'Spares', 
Year(DateAdd(Day,(lm.DeliveryARO + 90), GetDate())) AS YEDD, DatePart(Q, 
DateAdd(Day, (lm.DeliveryARO + 30), GetDate())) AS QEDD, lm.award, 
lm.PartNo, tb.tboid

FROM DBlm AS lm
Left Join DBtb as TB
ON lm.PartNo = tb.partno

WHERE lm.Entity = 'AAF' and lm.AwardDate is null and lm.award <>'Requirement Cancelled' and lm.ProposalDate is not null
GROUP BY Entity, DeliveryARO, award, lm.partno, tb.tboid, tb.partno

不工作查询:

SELECT sum(rm.Qty) as Proposed, rm.Entity as Entity, List = 'ROR',
Case WHEN rm.itemstatus = 'Additional Repair Proposal Submitted – Pending Award' then Year(DateADD(Day, (rm.ivFinalDelvTerms + 60), GetDate())) END AS YEDD,
Case When rm.itemstatus = 'Repair Proposal Submitted - Pending Award' then DatePart(Q, DateAdd(Day, (rm.iiDelvTerms + rm.ivFinalDelvTerms +90), GetDate())) END AS QEDD,
rm.award, tb.partno, tb.tboid

FROM DBrm AS rm
Inner Join DBtm as TB
ON rm.PartNo = tb.partno

where rm.ItemStatus like '%pending%award%' and 
tb.PartNo = rm.PartNo and rm.Entity = 'AAF' 

Group By Entity, award, tb.tboid, tb.partno, rm.IIDelvTerms, 
rm.IVFinalDelvTerms, rm.itemstatus, rm.qty

第二个查询返回正确的年份或YEDD,但Quarter或QEDD返回NULL。我已经尝试调整方程式,以便从添加日期中删除我的一个列,但它没有帮助。

我在其他任何地方都没有发现过这种情况。我见过人们建议转换为字符串,但包含iiDelvTerms和FinalDelvTerms的列已经是字符串格式。

对于我的错误可能会有任何想法或建议。

示例查询结果:

Proposed Entity List YEDD QEDD Award PartNo TBoId iiDelvTerms IVFinalDelvTerms
4         AAF   ROR 2018  NULL  M16 246     1        60        150
4         AAF   ROR 2018  NULL  M16 246     1        60        180
2         AAF   ROR 2018  NULL  M16 246     1        60        210
1         AAF   ROR 2018  NULL  M16 381     14       60        150

0 个答案:

没有答案