(CASE WHEN Status IN ('Selection Process','Approved','Closed') THEN
CASE WHEN TotalLayerApproval=1 THEN ApprDate1 ELSE
CASE WHEN TotalLayerApproval=2 THEN ApprDate2 ELSE
CASE WHEN TotalLayerApproval=3 THEN ApprDate3 ELSE
CASE WHEN TotalLayerApproval=4 THEN ApprDate4 ELSE
CASE WHEN TotalLayerApproval=5 THEN ApprDate5 ELSE
CASE WHEN TotalLayerApproval=6 THEN ApprDate6 ELSE
CASE WHEN TotalLayerApproval=7 THEN ApprDate7 ELSE
CASE WHEN TotalLayerApproval=8 THEN ApprDate8 ELSE
CASE WHEN TotalLayerApproval=9 THEN ApprDate9 ELSE NULL END
END
END
END
END
END
END
END
END
ELSE NULL END) AS Date_Approved,
(CASE WHEN Date_Approved IS NULL THEN NULL
ELSE DATEDIFF(day,Date_Approved, CONVERT(date,GETDATE())) END) AS SLA
例如上面的(
Date_Approved
),或者有人可以再次简化它,这样就不会出现太多情况
无需进行疯狂的嵌套......只需使用:
CASE
WHEN Status IN ('Selection Process', 'Approved', 'Closed')
THEN
CASE
WHEN TotalLayerApproval = 1 THEN ApprDate1
WHEN TotalLayerApproval = 2 THEN ApprDate2
WHEN TotalLayerApproval = 3 THEN ApprDate3
WHEN TotalLayerApproval = 4 THEN ApprDate4
WHEN TotalLayerApproval = 5 THEN ApprDate5
WHEN TotalLayerApproval = 6 THEN ApprDate6
WHEN TotalLayerApproval = 7 THEN ApprDate7
WHEN TotalLayerApproval = 8 THEN ApprDate8
WHEN TotalLayerApproval = 9 THEN ApprDate9
ELSE NULL
END
ELSE NULL
END AS Date_Approved,