如何在下面的“案例陈述”中添加“年限”?我只需要修复“INTERVAL 6 YEAR”& “间隔5年”标准。
以下是我需要建模的标准:
如果1〜5年,那么11
如果6〜10年,那么14
如果11年以上,那么18岁
CASE WHEN hiredate < DATE_SUB(NOW(),INTERVAL 11 YEAR)
THEN '18'
WHEN hiredate < DATE_SUB(NOW(),INTERVAL 6 YEAR)
THEN '14'
WHEN hiredate < DATE_SUB(NOW(),INTERVAL 5 YEAR)
THEN '11'
ELSE '0' END AS Non_Management_Accrual
答案 0 :(得分:1)
您可以使用DATEDIFF
方法计算两天之间的天数,并将其除以365以获得years
。基于这些,您可以编写case
语句,例如:
SELECT id, CASE WHEN FLOOR(DATEDIFF(NOW(), hiredate) / 365) < 1
THEN '0'
WHEN FLOOR(DATEDIFF(NOW(), hiredate) / 365) < 6
THEN '11'
WHEN FLOOR(DATEDIFF(NOW(), hiredate) / 365) < 11
THEN '14'
ELSE '18'
END AS Non_Management_Accrual
FROM emp;
这是SQL Fiddle。
答案 1 :(得分:0)
Darshan,我最终在MySQL端创建了Views,然后将视图添加到上面代码的FROM语句中。这给了我正确的输出。感谢。
<!--- Create the Manager Monthly Accruals (Mgt) dataset --->
<cfquery name="rsLeaveTimeManagerAccruals" datasource="care">
SELECT username, status, active, email, hiredate, tblUsers.picture AS eP
, TIMESTAMPDIFF(YEAR, hiredate, NOW()) AS year_passed
, TIMESTAMPDIFF(MONTH, hiredate, NOW()) MOD 12 AS month_passed
, TIMESTAMPDIFF(DAY, hiredate, NOW()) MOD 365 AS day_passed
, CONCAT(TIMESTAMPDIFF(YEAR, hiredate, NOW())," years, ",TIMESTAMPDIFF(MONTH, hiredate, NOW()) MOD 12," months, ",TIMESTAMPDIFF(DAY, hiredate, NOW()) MOD 365," days") AS EmployLengthActive
, TIMESTAMPDIFF(YEAR, hiredate, NOW()) AS monthly_accrual_time
, CASE WHEN FLOOR(DATEDIFF(NOW(), hiredate) / 365) < 1
THEN '0'
WHEN FLOOR(DATEDIFF(NOW(), hiredate) / 365) <= 5
THEN '14'
WHEN FLOOR(DATEDIFF(NOW(), hiredate) / 365) >= 6
THEN '18'
END AS Management_Accrual
FROM v_reports_to_mgt_list LEFT OUTER JOIN tblusers ON tblusers.ID = v_reports_to_mgt_list.id
WHERE clr_accrual = '1' AND hiredate IS NOT NULL
ORDER BY hiredate
</cfquery>