SQL - INSERT SELECT 两列不重复
SQL - INSERT SELECT two columns without duplicates
我正在尝试执行两个子查询以用两列填充我的 table。 wk_start
和 wk_end
。
目标是用 date
.
的特定范围填充它
我尝试过交叉连接,但仍然有与此相同的副本
这是我的代码
insert into bfs_dw.mpt_calendar (wk_start, wk_end)
select
ter.wk_start,
ter1.wk_end
from
(select * from
(select ADDDATE('2022-01-03', 7*(t4*10000 + t3*1000 + t2*100 + t1*10 + t0)) wk_start from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where wk_start < '2024-01-03'
order by wk_start ) ter
inner JOIN
(select * from
(select ADDDATE('2022-01-09', 7*(t4*10000 + t3*1000 + t2*100 + t1*10 + t0)) wk_end from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where wk_end < '2024-01-03'
order by wk_end ) ter1 ON 1 = 1
输出显示如下。插入后有很多重复项。
我想要做的是这个预期结果,一旦插入就没有重复:
注意:两个子查询效果很好。它的输出是这里发布的最后一张图片。
使用第一个子查询得到wk_start
,加上6天得到wk_end
insert into bfs_dw.mpt_calendar (wk_start, wk_end)
select wk_start, wk_start + INTERVAL 6 DAY wk_end from
(select ADDDATE('2022-01-03', 7*(t4*10000 + t3*1000 + t2*100 + t1*10 + t0)) wk_start from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where wk_start < '2024-01-03'
我正在尝试执行两个子查询以用两列填充我的 table。 wk_start
和 wk_end
。
目标是用 date
.
我尝试过交叉连接,但仍然有与此相同的副本
这是我的代码
insert into bfs_dw.mpt_calendar (wk_start, wk_end)
select
ter.wk_start,
ter1.wk_end
from
(select * from
(select ADDDATE('2022-01-03', 7*(t4*10000 + t3*1000 + t2*100 + t1*10 + t0)) wk_start from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where wk_start < '2024-01-03'
order by wk_start ) ter
inner JOIN
(select * from
(select ADDDATE('2022-01-09', 7*(t4*10000 + t3*1000 + t2*100 + t1*10 + t0)) wk_end from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where wk_end < '2024-01-03'
order by wk_end ) ter1 ON 1 = 1
输出显示如下。插入后有很多重复项。
我想要做的是这个预期结果,一旦插入就没有重复:
注意:两个子查询效果很好。它的输出是这里发布的最后一张图片。
使用第一个子查询得到wk_start
,加上6天得到wk_end
insert into bfs_dw.mpt_calendar (wk_start, wk_end)
select wk_start, wk_start + INTERVAL 6 DAY wk_end from
(select ADDDATE('2022-01-03', 7*(t4*10000 + t3*1000 + t2*100 + t1*10 + t0)) wk_start from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where wk_start < '2024-01-03'