如果同一剪辑存在相同日期,则提出能够添加时间间隔的逻辑#
Coming up with logic to be able to add a time interval if the same date exists for the same clip#
我正在尝试分离出具有相同日期的剪辑。例如,由于“剪辑编号”1 和 2 具有相同的日期,因此我试图通过添加 6 小时的间隔来使日期不同。我想添加一个类似于“新日期”列的新字段
Date
Clip_Number
New Date
1/2/2021
1
1/2/2021 0:00
1/2/2021
1
1/2/2021 0:00
1/2/2021
2
1/2/2021 6:00
1/2/2021
2
1/2/2021 6:00
1/3/2021
3
1/3/2021 0:00
1/3/2021
3
1/3/2021 0:00
假设只有两个值具有相同的日期,您可以使用:
select t.*,
(case when clip_number <> min(clip_number) over (partition by date)
then date + interval '6 hour'
else date
end)
from t;
Gordon 解决方案有效,但前提是“只有两个值具有相同的日期”。
即使在这种情况下,此解决方案也有效:
with data as (
select 1 clip, '2020-10-03' d
union all select 1, '2020-10-03'
union all select 1, '2020-10-03'
union all select 2, '2020-10-03'
union all select 2, '2020-10-03'
union all select 3, '2020-10-03'
union all select 3, '2020-10-04'
union all select 3, '2020-10-04'
)
select *
, timestampadd('hour'
, 6*(-1+dense_rank() over(partition by d order by clip))
, d
) new_date
from data
我正在尝试分离出具有相同日期的剪辑。例如,由于“剪辑编号”1 和 2 具有相同的日期,因此我试图通过添加 6 小时的间隔来使日期不同。我想添加一个类似于“新日期”列的新字段
Date | Clip_Number | New Date |
---|---|---|
1/2/2021 | 1 | 1/2/2021 0:00 |
1/2/2021 | 1 | 1/2/2021 0:00 |
1/2/2021 | 2 | 1/2/2021 6:00 |
1/2/2021 | 2 | 1/2/2021 6:00 |
1/3/2021 | 3 | 1/3/2021 0:00 |
1/3/2021 | 3 | 1/3/2021 0:00 |
假设只有两个值具有相同的日期,您可以使用:
select t.*,
(case when clip_number <> min(clip_number) over (partition by date)
then date + interval '6 hour'
else date
end)
from t;
Gordon 解决方案有效,但前提是“只有两个值具有相同的日期”。
即使在这种情况下,此解决方案也有效:
with data as (
select 1 clip, '2020-10-03' d
union all select 1, '2020-10-03'
union all select 1, '2020-10-03'
union all select 2, '2020-10-03'
union all select 2, '2020-10-03'
union all select 3, '2020-10-03'
union all select 3, '2020-10-04'
union all select 3, '2020-10-04'
)
select *
, timestampadd('hour'
, 6*(-1+dense_rank() over(partition by d order by clip))
, d
) new_date
from data