SQL: Find filled area [duplicate]
I have this table:
CREATE TABLE coordinates (
x INTEGER NOT NULL,
y INTEGER NOT NULL,
color VARCHAR(1) NOT NULL,
PRIMARY KEY(x,y)
)
Here are some sample data:
INSERT INTO coordinates
(x, y, color)
VALUES
(0, 4, 'g'),
(1, 0, 'g'),
(1, 1, 'g'),
(1, 2, 'g'),
(1, 3, 'g'),
(0, 4, 'g'),
(1, 0, 'g'),
(1, 1, 'g'),
(1, 2, 'g'),
(1, 3, 'g'),
(1, 4, 'g'),
(2, 0, 'b'),
(2, 1, 'g'),
(2, 2, 'g'),
(2, 3, 'g'),
(2, 4, 'g'),
(4, 0, 'b'),
(4, 1, 'r'),
(4, 2, 'r'),
(4, 3, 'g'),
(4,开发者_开发技巧 4, 'g'),
(6, 0, 'r'),
(6, 1, 'g'),
(6, 2, 'g'),
(6, 3, 'r'),
(6, 4, 'r')
;
I am trying to write a query that finds all the largest-area rectangles. This is assuming a rectangle is defined by its bottom left and top right, and that 1/4 is r, 1/4 is b, 1/4 is g, 1/4 is y.
So result should be something similarly like this:
x1 | y1 | x2 | y2 | area
-------------------------
0 1 6 9 58
1 2 4 7 58
Make a function that calculates the area and then query the function to get the biggest one.
精彩评论