繁体   English   中英

SQL查询以生成给定日期范围内的员工缺席报告

[英]SQL Query to generate Employee absent report for a given range of dates

我有两个由所有雇员组成的表Master(empname,empid)和另一个表Transaction(empid,Presentdate),该表提供了日期和哪个雇员在场。
如何找到特定日期范围内的缺席者列表

我已经尝试了下面的SQL查询

SELECT empid FROM Master 
where empid NOT IN(select empid  from  Transaction 
where Date(Presentdate) between '2012-11-21' and '2012-12-22')

它只返回缺席的员工ID,我也要显示该员工的缺席日期

注意(事务表仅存储员工的当前日期)

如果没有员工,则整个记录将不会插入到“交易”表中

尚未测试,但这可能有效:

SELECT m.empid,d.Presentdate

FROM Master as m,
(select distinct Presentdate from transaction
where Date(Presentdate) between '2012-11-21' and '2012-12-22') as d 

where m.empid not in (select empid  from  Transaction 
where Presentdate=d.Presentdate)

这应该工作,未经测试

SELECT m.empid     AS `Empid` 
     , d.dt        AS `AbsentDate`
  FROM ( SELECT DATE(t.Presentdate) AS dt
           FROM transaction t
          WHERE t.Presentdate >= '2012-11-21' 
            AND t.Presentdate < DATE_ADD( '2012-12-22' ,INTERVAL 1 DAY)
          GROUP BY DATE(t.Presentdate)
          ORDER BY DATE(t.Presentdate)
       ) d
 CROSS
  JOIN master m
  LEFT
  JOIN  transaction p
    ON p.Presentdate >= d.dt
   AND p.Presentdate <  d.dt + INTERVAL 1 DAY
   AND p.empid = m.empid
 WHERE p.empid IS NULL
 ORDER
    BY m.empid
     , d.dt

//我想您的查询会这样获取雇员姓名,身份证和他/她在职的日期。

select empid,empname,date  from  Transaction LEFT JOIN Master ON empid 
where Date(Presentdate) between '2012-11-21' and '2012-12-22'

//为循环目的创建了一个数组。 您还将拥有emp id和名称

$array = array('2012-01-01','2012-01-02','2012-01-03','2012-01-05'); 
$date = new DateTime('2012-01-01'); // take first as start date
$startDate = $array[0]; 
$endDate = $array[count($array)-1];
$loop = 'yes';

while($loop == 'yes') {
    $date->add(new DateInterval('P1D'));    //add one day to start date
    if(!in_array($date->format('Y-m-d'),$array))
        $absentDate = $date->format('Y-m-d');
    if($date->format('Y-m-d') == $endDate)
        $loop = 'no';
}
print_r($absentDate);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM