M
M
Mikha Pankratov2017-10-13 15:41:33
MySQL
Mikha Pankratov, 2017-10-13 15:41:33

How to count holes?

good afternoon,
a task to stretch the brain.
there is a plate
d
---
1
2
3
5
6
7
10
11
15
21
you need to calculate how many holes there will be, namely (4) - I will show where
d
---
1
2
3
-----------
5
6
7
-----------
10
11
-----------
15
-----------
21
need to write request

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SharuPoNemnogu, 2017-10-13
@SharuPoNemnogu

SELECT count(
  CASE WHEN NOT exists(
    SELECT null FROM table t2 WHERE t2.d = t1.d + 1
  ) THEN 1 END
) - 1
FROM table t1;

SELECT count(t1.d) - 1
FROM table t1
LEFT JOIN table t2 ON t2.d = t1.d + 1
WHERE t2.d IS NULL;

M
Mikha Pankratov, 2017-10-13
@frmax

select sum(1)-1 from tt t1 where not exists (select * from tt where id - 1 = t1.id);
I'll give you my solution too.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question