SQL Query在给定的日期范围内生成Employee缺席报告

时间:2012-12-26 09:20:27

标签: java mysql sql

我有两个表Master(empname,empid),包括所有员工和另一个表Transaction(empid,Presentdate),它提供日期和员工在场。
如何在特定日期范围内找到缺席者名单

我已经尝试过以下的Sql Query

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

只返回缺席的员工ID,我想显示员工缺席的日期

注意(交易表仅存储员工的当前日期)

如果员工不在,那么整个记录将不会插入到交易表中

3 个答案:

答案 0 :(得分:0)

未对此进行测试,但这可能有效:

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)

答案 1 :(得分:0)

这应该可以使用,未经测试

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

答案 2 :(得分:0)

//我想你的查询会喜欢这个,以获得他/她在场的员工姓名,身份证明和日期。

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);