【问题标题】:Only show rows where certain data does not match仅显示某些数据不匹配的行
【发布时间】:2021-05-07 19:19:17
【问题描述】:

我有一个表,其中包含员工是否已登录工作。当他们登录和注销时会插入一个新行。我想要做的是今天日期尚未注销的员工的结果。

表结构如下:

Employee_Ref ShiftDate In_or_Out
191 2021-05-07 00:00:00.000 I
320 2021-05-07 00:00:00.000 I
320 2021-05-07 00:00:00.000 O

需要的结果是:

Employee_Ref ShiftDate In_or_Out
191 2021-05-07 00:00:00.000 I

有人可以帮忙吗?

【问题讨论】:

  • 这是什么SQL实现?您是否尝试过任何查询并遇到任何具体问题?
  • 你还在纠结IO的用法吗?为什么不用+1-1 替换它呢?然后您可以对 In_Or_Out 列和 GROUP BY Employee_Ref、ShiftDate 求和,并使用 SUM() 0HAVING 子句

标签: sql


【解决方案1】:

我想要做的是今天日期尚未注销的员工的结果。

大概,您想要在今天登录的人。我会假设是这样的。

如果你想要员工,你可以使用聚合:

select employee_ref
from t
where shiftdate >= current_date and
      shiftdate < current_date + interval '1 day'
group by employee_ref
having sum(case when in_or_out = 'O' then 1 else 0 end) = 0;

如果你想要详细的行,你可以使用not exists

select t.*
from t
where shiftdate >= current_date and
      shiftdate < current_date + interval '1 day' and
      not exists (select 1
                  from t t2
                  where t2.employee_ref = t.employee_ref and
                        t2.shiftdate >= current_date and
                        t2.shiftdate < current_date + interval '1 day' and
                        t2.in_or_out = 'O'
                 );

请注意,日期函数因数据库而异。这使用可以适应您的数据库的标准 SQL 语法。

【讨论】:

  • 您好,谢谢,这在一定程度上有效,但员工可以在一天中多次登录和注销
  • @BobJones 。 . .我建议您提出一个新的问题,并附上适当的样本数据、期望的结果和解释。
  • 排序感谢'select Employee_Ref from CLOCKING where Shift_Date >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) and Shift_Date sum(case when In_or_Out = 'O' then 1 else 0 end);'
猜你喜欢
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-27
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
相关资源
最近更新 更多