-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShow missing records.sql
More file actions
49 lines (39 loc) · 1.3 KB
/
Show missing records.sql
File metadata and controls
49 lines (39 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# =======================================
# Select gaps that are missed in sequence
# =======================================
Select (t1.num + 1) as gapStart
, (Select min(t3.num) - 1
from tutorial.sequences t3
Where t3.num > t1.num) as gapEnd
From tutorial.sequences t1
Where not exists (
Select t2.num
from tutorial.sequences as t2
where t2.num = t1.num + 1)
Having gapEnd is not null;
# ==========================================
# Select numbers that are missed in sequence
# ==========================================
CREATE TEMPORARY TABLE IF NOT EXISTS temporaryTable (id int);
DELIMITER $$
drop procedure if exists fillTemporaryTableWithInterval$$
Create procedure fillTemporaryTableWithInterval(in minVal int, in maxVal int)
begin
declare counter int default minVal;
while counter < maxVal DO
insert into temporaryTable (id)
values (counter);
set counter := counter + 1;
end while;
end$$
DELIMITER ;
set @minID := 1;
Select @maxID := max(num) from tutorial.sequences;
call fillTemporaryTableWithInterval(@minID, @maxID);
Select t1.id
From temporaryTable t1
left join tutorial.sequences as t2
on t1.id = t2.num
where t2.num is null;
drop procedure if exists fillTemporaryTableWithInterval;
Drop table if exists temporaryTable;