查找基于ID不存在的记录

时间:2016-11-01 16:09:18

标签: sql oracle exists

当员工没有时间输入存储在名为employee_time et的表中的时间卡时,我需要报告。

我的问题是找到那些没有参加上周时间的人。

我已使用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

这也很有效,但我需要嫁给他们,但迄今为止都没有成功。你能帮忙吗?

4 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

一般格式如下:

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

答案 2 :(得分: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是一个胜利。

答案 3 :(得分: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
);