2016-11-01 46 views
0

我需要向他们报告的员工没有存储在一个名为employee_time等表中的考勤卡输入的时间。查找记录这不/的确存在

我的问题是要找到谁没有进入时间说周的人。

我已经使用了NOT IN发现不存在于表中,因此,如果已经输入记录不存在任何时候员工ID。这很好。

Select 
    e.userName, 
    et.dateStart, 
    et.dateEnd 

From employee_time et, 
     employee e 
Where 
     e.employee_id NOT IN (Select employee_id from employee_time 
          where et.dateStart = date and 
            et.end_date = date) 
Group by 
     e.userName, 
     et.startDate, 
     et.endDate 

的问题是应用程序,当一个人导航到员工的时间输入页面它创造纪录的员工,如果员工不进入任何时间,结果是零小时的记录。但是这个记录现在存在。因此,不会在上面的查询可以发现,我已经使用了以下查找其总计记录= 0

Select 
    e.userName, 
    et.dateStart, 
    et.dateEnd, 
    SUM(et.hoursTotal) AS total 

From employee_time et, 
     employee e 
Where 
     e.employee_id = et.employee_id and 
     et.hours_total = 0 
Group by 
     e.userName, 
     et.startDate, 
     et.endDate 

这也非常实用,但是我需要与他们结婚,但一直不成功的日期。你能帮我吗?

+0

你在这两个查询的“联姻”想什么实际输出? –

+0

你想用'hours_total' = 0来对待记录,就好像它们不存在一样? 'hours_total <> 0'? –

+0

我想要的输出是employee_ids,所以我能够访问用户名并将其放入报告中,表示没有输入时间。 – Bman

回答

0
Select 
    e.userName, 
    et.dateStart, 
    et.dateEnd, 
    SUM(et.hoursTotal) AS total 

From 
     employee e left outer join employee_time et on (e.employee_id = et.employee_id) 
-- Where nvl(et.hours_total,0) = 0 
Group by 
     e.userName, 
     et.startDate, 
     et.endDate 
having SUM(et.hoursTotal) > 0 

如果你不希望有您可以使用作了评论,这应该是相同的

,如果你只想要employee_ids:

select e.empolyee_id 
from employee e left outer join employee_time et on (e.employee_id = et.employee_id) 
Where nvl(et.hours_total,0) = 0; 
0

一般格式是这样的:

Select employee_id 
    From employee 
Minus 
Select employee_id 
    From TableExpressionEliminatesOnMissingDates 
Minus 
Select employee_id 
    From TableExpressionEliminatesOnSummarization; 
0

我会为这样写:

Select e.userName 
From employee e 
Where e.employee_id NOT IN (Select employee_id 
          from employee_time 
          where et.dateStart = $date and 
            et.end_date = $date and 
            et.hoursTotal > 0 
          ); 

您从开始的日期作为输入,所以我不明白为什么你需要它的输出也是如此。如果你这样做,我会去:

Select e.userName, w.* 
From employee e cross join 
    (select distinct et.dateStart, et.end_date 
     from employee_time et 
     where $date between et.dateStart and et.dateEnd 
    ) w 
Where e.employee_id NOT IN (Select et.employee_id 
          from employee_time et 
          where $date between et.dateStart and et.dateEnd and 
            et.hoursTotal > 0 
          ); 

注:在外部查询。避免GROUP BY是在性能方面的胜利。

0

以下查询发现,在employee_time对于给定的开始和结束日期没有有效的条目中的所有员工:

select * 
from employee 
where employee_id not in 
(
    select employee_id 
    from employee_time 
    where datestart = :date_start 
    and end_date = :date_end 
    and hours_total > 0 
); 
+0

Thorsten,这是正确的,但我也需要那些从不访问employee_time表的人员,因此他们没有记录。 – Bman

+0

查询正在查找在给定时间范围内employee_time中没有有效条目的员工。对于当然没有任何记录的员工也是如此。你甚至尝试过查询吗? –