Sql 计算表中的当前和缺席天数
我有一个表名Sql 计算表中的当前和缺席天数,sql,sql-server-2008,tsql,unpivot,Sql,Sql Server 2008,Tsql,Unpivot,我有一个表名Emp\u mon\u day,它由员工在场的和缺席的详细信息组成 我想要的是 我需要这9名员工的Emp_mon_day表中每个员工的当前天数和缺勤天数信息,合并到下面的查询中 查询 SELECT e.comp_mkey, e.status, e.resig_date, dt_of_leave, e.emp_name, e.date_of_joining, e.emp_card_no, a.pl_days, pl_days_openin
Emp\u mon\u day
,它由员工在场的和缺席的详细信息组成
我想要的是
我需要这9名员工的Emp_mon_day
表中每个员工的当前天数和缺勤天数信息,合并到下面的查询中
查询
SELECT e.comp_mkey,
e.status,
e.resig_date,
dt_of_leave,
e.emp_name,
e.date_of_joining,
e.emp_card_no,
a.pl_days,
pl_days_opening,
a.month1,
a.month2,
a.month3,
a.month4,
a.month5,
a.month6,
a.month7,
a.month8,
a.month9,
a.month10,
a.month11,
a.month12,
a.month1 + a.month2 + a.month3 + a.month4 + a.month5 + a.month6 + a.month7 + a.month8 + a.month9 + +a.month10 + a.month11 + a.month12 AS pl_sum
FROM p_leave_allocation AS a
INNER JOIN
emp_mst AS e
ON a.emp_card_no = e.emp_card_no
WHERE a.year = 2016
AND (datediff(MONTH, e.date_of_joining, CONVERT (DATETIME, getdate(), 103)) >= 6
AND datediff(MONTH, e.date_of_joining, CONVERT (DATETIME, getdate(), 103)) <= 36)
AND (e.resig_date IS NULL
OR (e.dt_of_leave IS NOT NULL
AND e.dt_of_leave >= CONVERT (DATETIME, getdate(), 103)))
AND e.status IN ('A', 'S')
AND e.comp_mkey IN (7, 110)
AND a.Year = 2016;
选择e.comp\u mkey,
e、 地位,
e、 辞职日期,
离开的时候,
e、 emp_名称,
e、 加入的日期,
e、 emp卡编号,
a、 五天,
开业日,
a、 月1日,
a、 月2日,
a、 月3日,
a、 月4日,
a、 月5日,
a、 月6日,
a、 月7日,
a、 月8日,
a、 月9日,
a、 10月,
a、 11月11日,
a、 12月12日,
a、 月1+a.month2+a.month3+a.month4+a.month5+a.month6+a.month7+a.month8+a.month9++a.month10+a.month11+a.month12作为损益和
从p_休假_分配为
内连接
emp_mst AS e
在a.emp\u卡号上=e.emp\u卡号
其中a.年=2016年
和(datediff(月,即加入的日期),转换(DATETIME,getdate(),103))>=6
和datediff(月,例如加入的日期,CONVERT(DATETIME,getdate(),103))=CONVERT(DATETIME,getdate(),103)))
和e.在('A','S'中的状态)
和e.comp_mkey IN(7110)
a.年份=2016年;
上面的查询为我提供了如下数据
[![图像数据][1][1]
Emp\u mon\u day
的列详细信息如下
[![enter image description here][2][2]您可以尝试以下查询:
SELECT e.comp_mkey, e.status, e.resig_date, dt_of_leave, e.emp_name,
e.date_of_joining, e.emp_card_no, a.pl_days, pl_days_opening, a.month1,
a.month2, a.month3, a.month4, a.month5, a.month6, a.month7, a.month8,
a.month9, a.month10, a.month11, a.month12,
a.month1 + a.month2 + a.month3 + a.month4 + a.month5 + a.month6 + a.month7 + a.month8 + a.month9 + +a.month10 + a.month11 + a.month12 AS pl_sum,
m.[DaysAbsent],m.[DaysPresent]
FROM p_leave_allocation AS a
INNER JOIN
emp_mst AS e
ON a.emp_card_no = e.emp_card_no
INNER JOIN
(
SELECT
comp_mkey,emp_mkey,[month],[year],
SUM(CASE WHEN data ='AB' THEN 1 ELSE 0 END) AS [DaysAbsent],
SUM(CASE WHEN data ='P' THEN 1 ELSE 0 END) AS [DaysPresent]
FROM
(
SELECT comp_mkey,emp_mkey,[month],[year],[Day1],[Day2],[Day3],[Day4],[Day5]
--,...
FROM Emp_mon_day
) source
UNPIVOT
(
data FOR day IN ([Day1],[Day2],[Day3],[Day4],[Day5]) -- dynamic query can generate all days data
)up
GROUP BY comp_mkey, emp_mkey,[month],[year]
) AS m
ON m.comp_mkey=e.Comp_mkey and m.emp_mkey=e.mkey
--- ABOVE CRITERIA NEEDS TO BE CHECKED
WHERE a.year = 2016
AND (datediff(MONTH, e.date_of_joining, CONVERT (DATETIME, getdate(), 103)) >= 6
AND datediff(MONTH, e.date_of_joining, CONVERT (DATETIME, getdate(), 103)) <= 36)
AND (e.resig_date IS NULL
OR (e.dt_of_leave IS NOT NULL
AND e.dt_of_leave >= CONVERT (DATETIME, getdate(), 103)))
AND e.status IN ('A', 'S')
AND e.comp_mkey IN (7, 110)
AND a.Year = 2016;
输出格式必须是什么?是否要为所有人重复这9条记录中的每一条days@DhruvJoshi当前位置Naah,查看当前员工、缺席员工的计算我想在本查询中合并这些员工在这种情况下,您想计算当前/缺席员工的数量吗days@DhruvJoshi:几乎是的,上面的查询为我提供了9名员工的输出。因此,对于这9名员工,我希望通过在上述查询中合并显示每位员工的出席/缺席详细信息,这意味着您在
emp\u mon\u day
中没有emp\u card\u no
列。请让我知道Emp\u mon\u day
和Emp\u mst
@coder之间的连接标准。除此之外,我无法排除故障,因为我无法访问架构和数据。请通过执行内部查询进行故障排除,以查看是否生成任何数据。如果是这样的话,下一件事就是检查联接条件,以便正确地传递数据。因为您现在得到0条记录,所以内部联接是罪魁祸首,因为它不匹配两个表之间的任何内容。
WHERE comp_mkey IN (7, 110) AND Year = 2016;