Sql 将bigint转换为日期
我有两个日期,我计算如下:Sql 将bigint转换为日期,sql,sql-server,Sql,Sql Server,我有两个日期,我计算如下: CASE WHEN FinalApprovalDt is not null and mf.IncomingTocontrol = 1 THEN DATEDIFF ( SECOND , ManualDecisionDt , lastChangesByAgentDt) END AS M_ttp CASE WHEN FinalApprovalDt is not null and mf.IncomingTocontrol = 0 and ContractPayoutAmt
CASE WHEN FinalApprovalDt is not null and mf.IncomingTocontrol = 1 THEN DATEDIFF ( SECOND , ManualDecisionDt , lastChangesByAgentDt) END AS M_ttp
CASE WHEN FinalApprovalDt is not null and mf.IncomingTocontrol = 0 and ContractPayoutAmt > 0 THEN DATEDIFF ( SECOND , SystemDecisionDt , lastChangesByAgentDt) END AS A_ttp
我需要在秒内得到这两个的总量,然后计算平均值,但当:
(Case When FinalApprovalDt is not null and mf.IncomingTocontrol = 1 and ContractPayoutAmt > 0 then DATEDIFF ( SECOND , ManualDecisionDt , lastChangesByAgentDt) End +
Case When FinalApprovalDt is not null and mf.IncomingTocontrol = 0 and ContractPayoutAmt > 0 then DATEDIFF ( SECOND , SystemDecisionDt , lastChangesByAgentDt) End ) As Total_TTP,
有什么问题吗?在该查询中有两个相互矛盾的情况,当条件不满足时,您不处理这些情况。基于
mf.IncomingTocontrol
第一种或第二种情况都将为NULL,NULL+任何内容都始终为NULL
您应该在每种情况下放置ELSE块:
(
Case When FinalApprovalDt is not null and mf.IncomingTocontrol = 1 and ContractPayoutAmt > 0
then DATEDIFF ( SECOND , ManualDecisionDt , lastChangesByAgentDt)
ELSE 0 End
+
Case When FinalApprovalDt is not null and mf.IncomingTocontrol = 0 and ContractPayoutAmt > 0
then DATEDIFF ( SECOND , SystemDecisionDt , lastChangesByAgentDt)
ELSE 0 End
) As Total_TTP,
如果为NULL,则使用COALESCE对值进行换行
(
COALESCE(Case When FinalApprovalDt is not null and mf.IncomingTocontrol = 1 and ContractPayoutAmt > 0
then DATEDIFF ( SECOND , ManualDecisionDt , lastChangesByAgentDt) End, 0)
+
COALESCE(Case When FinalApprovalDt is not null and mf.IncomingTocontrol = 0 and ContractPayoutAmt > 0
then DATEDIFF ( SECOND , SystemDecisionDt , lastChangesByAgentDt) End, 0)
) As Total_TTP,
编辑您的问题并提供示例数据和所需结果。您可能缺少一两个
ELSE 0
<代码>x时的情况,然后。。。当x
为非真时,END为NULL
,向任何内容添加NULL
为NULL
。