想在mysql中按月计算休假天数
Posted
技术标签:
【中文标题】想在mysql中按月计算休假天数【英文标题】:Want to calculate leave days month wise in mysql 【发布时间】:2016-03-30 06:32:49 【问题描述】:如果员工申请休假:From Date 是“2016-03-30”,To Date 是“2016-04-02”,那么输出将是“In 3rd month leave days should be 2”和“In 4th月假天数应为 1"。 我有表:
UserID FromDate ToDate LeaveDuration
------------------------------------------------------
0001 20/03/2016 21/03/2016 1
0001 30/03/2016 02/04/2016 2
在第 2 条记录中,第 3 个月应考虑 2 天,第 4 个月应考虑 1 天。 我尝试了以下查询:
select sum(datediff(ToDate,FromDate)) as Total
from wp_ag_assign_leave
where UserId=18
and LeaveType="Carry Forward Leave"
and (EXTRACT(MONTH FROM CURDATE())=EXTRACT(MONTH FROM FromDate)
OR EXTRACT(MONTH FROM CURDATE())=EXTRACT(MONTH FROM ToDate))
请帮我解决一下
【问题讨论】:
什么?请用一些数据样本和预期输出更新您的问题 尝试每月使用 GROUP BY,一个有用的链接 ***.com/a/18087708/4229270 您的查询提供的输出与您期望的相反。 ***.com/a/9888386/4229270 更严重的是,您的示例中有一些奇怪的地方,它缺少一个重要的规则:确实,第一行 20/03 和 21/03 是 1 天。第二行 30/03 31/03 01/04 和 04/04 为 2 天。它应该是 3,除非考虑到休息日(如我所说) 【参考方案1】:您可能想要精确一些日期,例如 date_add
,但这是我所做的:
select month,
sum(duration) as 'LeaveDays'
from (
select if(month(FromDate)=month(ToDate),month(FromDate),'other') as 'month',
if(month(FromDate)=month(ToDate),datediff(Todate, FromDate),'other') as 'duration'
from wp_ag_assign_leave
UNION
select if(month(FromDate)!=month(ToDate),month(FromDate),'other') as 'month',
if(month(FromDate)!=month(ToDate),datediff(last_day(FromDate),FromDate),'other') as 'duration'
from wp_ag_assign_leave
UNION
select if(month(FromDate)!=month(ToDate),month(ToDate),'other') as 'month',
if(month(FromDate)!=month(ToDate),dateDiff(ToDate,last_day(FromDate)),'other') as 'duration'
from wp_ag_assign_leave
) as parseMonths
where month!='other'
group by month
order by month;
UNION
语句中的 3 SELECT
:
- 1 日期在同一个月,简单的区别
- 2日期不在同一个月:第一个月的部分
- 2 日期不在同一个月:第二个月的部分
【讨论】:
【参考方案2】:根据场景计算每月和每年的休假天数
select user_id,ymonth,
sum(duration) as 'LeaveDays'
from (
select user_id,if(EXTRACT(YEAR_MONTH FROM leave_from)=EXTRACT(YEAR_MONTH FROM leave_to),EXTRACT(YEAR_MONTH FROM leave_from),'other') as 'ymonth',
if(EXTRACT(YEAR_MONTH FROM leave_from)=EXTRACT(YEAR_MONTH FROM leave_to),datediff(leave_to, leave_from)+1,'other') as 'duration'
from leave_application
UNION ALL
select user_id, if(EXTRACT(YEAR_MONTH FROM leave_from)!=EXTRACT(YEAR_MONTH FROM leave_to),EXTRACT(YEAR_MONTH FROM leave_from),'other') as 'ymonth',
if(EXTRACT(YEAR_MONTH FROM leave_from)!=EXTRACT(YEAR_MONTH FROM leave_to),datediff(last_day(leave_from),leave_from)+1,'other') as 'duration'
from leave_application
UNION ALL
select user_id, if(EXTRACT(YEAR_MONTH FROM leave_from)!=EXTRACT(YEAR_MONTH FROM leave_to),EXTRACT(YEAR_MONTH FROM leave_to),'other') as 'ymonth',
if(EXTRACT(YEAR_MONTH FROM leave_from)!=EXTRACT(YEAR_MONTH FROM leave_to),dateDiff(leave_to,last_day(leave_from)),'other') as 'duration'
from leave_application
) as parseMonths
where ymonth!='other'
group by ymonth
order by ymonth
【讨论】:
正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。以上是关于想在mysql中按月计算休假天数的主要内容,如果未能解决你的问题,请参考以下文章