删除 INSERT SELECT 语句中的重复查询
Remove dupilcates query in INSERT SELECT statement
我有一个查询,它通过 INSERT SELECT 语句将数据从任务 Table 插入 Clockify Table。
ALTER procedure [dbo].[ClockifyAdd]
AS
BEGIN
insert into Clockify(ClockifyId,DurationInMinutes,Date)
SELECT
ClockifyId
, SUM(DATEDIFF(mi, CAST(StartTime AS datetime), CAST(EndTime AS
datetime))) AS DurationInMinutes
, CAST(StartTime AS date) AS Date
FROM Task
GROUP BY
ClockifyId
, CAST(StartTime AS date)
END
问题出在插入 Clockify 时 table 我不想插入重复项并在此处进行检查。我有一个删除重复项的存储过程,但我想要一种更好的方法,即在插入时必须检查重复项,如果有重复项,则不得插入。请查看图片以便更好地理解
提前感谢您的回复并抽出宝贵的时间来解决这个问题。
enter image description here
您可以使用左连接检查空值来检查值是否已经存在
insert into Clockify(ClockifyId,DurationInMinutes,Date)
SELECT
Task.ClockifyId
, SUM(DATEDIFF(mi, CAST(Task.StartTime AS datetime), CAST(Task.EndTime AS
datetime))) AS DurationInMinutes
, CAST(Task.StartTime AS date) AS Date
FROM Task
LEFT JOIN Clockify
ON Task.ClockifyId = Clockify.ClockifyId
AND Task.Date = Clockify.Date
WHERE Clockify.ClockifyId Is NULL
GROUP BY
Task-ClockifyId
, CAST(Task.StartTime AS date)
您可以使用 not exists 如下:
insert into Clockify(ClockifyId,DurationInMinutes,Date)
select t.* from
(SELECT
ClockifyId
, SUM(DATEDIFF(mi, CAST(StartTime AS datetime), CAST(EndTime AS
datetime))) AS DurationInMinutes
, CAST(StartTime AS date) AS Date
FROM Task
GROUP BY
ClockifyId
, CAST(StartTime AS date) ) t
where not exists
(select 1 from Clockify c
where c.ClockifyId = t.ClockifyId
and c.DurationInMinutes = t.DurationInMinutes
and c.Date = t.Date)
-- remove the condition on column from here if you don't want to
-- consider column for finding duplicate
我有一个查询,它通过 INSERT SELECT 语句将数据从任务 Table 插入 Clockify Table。
ALTER procedure [dbo].[ClockifyAdd]
AS
BEGIN
insert into Clockify(ClockifyId,DurationInMinutes,Date)
SELECT
ClockifyId
, SUM(DATEDIFF(mi, CAST(StartTime AS datetime), CAST(EndTime AS
datetime))) AS DurationInMinutes
, CAST(StartTime AS date) AS Date
FROM Task
GROUP BY
ClockifyId
, CAST(StartTime AS date)
END
问题出在插入 Clockify 时 table 我不想插入重复项并在此处进行检查。我有一个删除重复项的存储过程,但我想要一种更好的方法,即在插入时必须检查重复项,如果有重复项,则不得插入。请查看图片以便更好地理解 提前感谢您的回复并抽出宝贵的时间来解决这个问题。 enter image description here
您可以使用左连接检查空值来检查值是否已经存在
insert into Clockify(ClockifyId,DurationInMinutes,Date)
SELECT
Task.ClockifyId
, SUM(DATEDIFF(mi, CAST(Task.StartTime AS datetime), CAST(Task.EndTime AS
datetime))) AS DurationInMinutes
, CAST(Task.StartTime AS date) AS Date
FROM Task
LEFT JOIN Clockify
ON Task.ClockifyId = Clockify.ClockifyId
AND Task.Date = Clockify.Date
WHERE Clockify.ClockifyId Is NULL
GROUP BY
Task-ClockifyId
, CAST(Task.StartTime AS date)
您可以使用 not exists 如下:
insert into Clockify(ClockifyId,DurationInMinutes,Date)
select t.* from
(SELECT
ClockifyId
, SUM(DATEDIFF(mi, CAST(StartTime AS datetime), CAST(EndTime AS
datetime))) AS DurationInMinutes
, CAST(StartTime AS date) AS Date
FROM Task
GROUP BY
ClockifyId
, CAST(StartTime AS date) ) t
where not exists
(select 1 from Clockify c
where c.ClockifyId = t.ClockifyId
and c.DurationInMinutes = t.DurationInMinutes
and c.Date = t.Date)
-- remove the condition on column from here if you don't want to
-- consider column for finding duplicate