如何按特定日期和另一个标识符对我的数据进行分区 SQL
How to partition my data by a specific date and another identifier SQL
with cte as
(
select to_date('01-JUN-2020','DD-MON-YYYY')+(level-1) DT
from dual
connect bY level<= 30
)
select *
from cte x
left outer join
(select date from time where emp in (1, 2)) a on x.dt = a.date
在这种情况下,我试图找到这些人没有报告工作的缺失天数......它适用于 1 人。我正确地找回了他们失踪的日子。但是当我添加 2 个人时.. 我没有为他们找回正确的缺失日期,因为我猜我只是在日期加入。
我想知道如何按人员 ID 和日期对这些数据进行分区,以便获得每个人丢失的准确日期。
请帮忙,谢谢。
您通常会 cross join
日期列表和人员列表,然后使用 not exists
提取缺少的 person/date 元组:
with cte as (
select date '2020-06-01' + level - 1 dt
from dual
connect by level <= 30
)
select c.dt, e.emp
from cte c
cross join (select distinct emp from times) e
where not exists (
select 1
from times t
where t.emp = e.emp and t.dt = e.date
)
请注意,这里使用的是文字日期而不是 to_date()
,后者在这里更合适。
这一次给出了所有人的缺失元组。如果您只需要预定义的人员列表,则:
with cte as (
select date '2020-06-01' + level - 1 dt
from dual
connect by level <= 30
)
select c.dt, e.emp
from cte c
cross join (select 1 emp from dual union all select 2 from dual) e
where not exists (
select 1
from times t
where t.emp = e.emp and t.dt = e.date
)
如果您还想查看“存在”日期,请使用 left join
而不是 not exists
,如在您的原始查询中:
with cte as (
select date '2020-06-01' + level - 1 dt
from dual
connect by level <= 30
)
select c.dt, e.emp, -- enumerate the relevant columns from "t" here
from cte c
cross join (select 1 emp from dual union all select 2 from dual) e
left join times t on t.emp = e.emp and t.dt = e.date
with cte as
(
select to_date('01-JUN-2020','DD-MON-YYYY')+(level-1) DT
from dual
connect bY level<= 30
)
select *
from cte x
left outer join
(select date from time where emp in (1, 2)) a on x.dt = a.date
在这种情况下,我试图找到这些人没有报告工作的缺失天数......它适用于 1 人。我正确地找回了他们失踪的日子。但是当我添加 2 个人时.. 我没有为他们找回正确的缺失日期,因为我猜我只是在日期加入。
我想知道如何按人员 ID 和日期对这些数据进行分区,以便获得每个人丢失的准确日期。
请帮忙,谢谢。
您通常会 cross join
日期列表和人员列表,然后使用 not exists
提取缺少的 person/date 元组:
with cte as (
select date '2020-06-01' + level - 1 dt
from dual
connect by level <= 30
)
select c.dt, e.emp
from cte c
cross join (select distinct emp from times) e
where not exists (
select 1
from times t
where t.emp = e.emp and t.dt = e.date
)
请注意,这里使用的是文字日期而不是 to_date()
,后者在这里更合适。
这一次给出了所有人的缺失元组。如果您只需要预定义的人员列表,则:
with cte as (
select date '2020-06-01' + level - 1 dt
from dual
connect by level <= 30
)
select c.dt, e.emp
from cte c
cross join (select 1 emp from dual union all select 2 from dual) e
where not exists (
select 1
from times t
where t.emp = e.emp and t.dt = e.date
)
如果您还想查看“存在”日期,请使用 left join
而不是 not exists
,如在您的原始查询中:
with cte as (
select date '2020-06-01' + level - 1 dt
from dual
connect by level <= 30
)
select c.dt, e.emp, -- enumerate the relevant columns from "t" here
from cte c
cross join (select 1 emp from dual union all select 2 from dual) e
left join times t on t.emp = e.emp and t.dt = e.date