Given the following filled x, y coordinates:
0, 0
0, 1
0, 2
1, 0
1, 1
1, 2
2, 0
2, 1
2, 2
4, 0
4, 1
5, 0
5, 1
How do I write an SQL query to determine all the filled rectangles? A rectangle is defined by its top left and bottom right corners.
Desired Results
x1 | y1 | x2 | y2
0 0 2 2
0 4 1 5
Because
+---+---+---+---+
| | 0 | 1 | 2 |
+---+---+---+---+
| 0 | X | X | X |
| 1 | X | X | X |
| 2 | X | X | X |
| 3 | | | |
| 4 | X | X | |
| 5 | X | X | |
+---+---+---+---+
Interesting puzzle, solution edited with ideas from ypercube's answer:
It first builds a list of all possible rectangles. Then it demands that the number of filled positions matches the total number of positions (the area of the rectangle.) Finally, it demands that there's no other rectangle that entirely covers the rectangle.
You might have to adopt it for PostgreSQL, but the idea should work.