SQL:查找填充区域
我有这个表:
CREATE TABLE coordinates (
x INTEGER NOT NULL,
y INTEGER NOT NULL,
color VARCHAR(1) NOT NULL,
PRIMARY KEY(x,y)
)
这里有一些示例数据:
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')
;
我正在尝试编写一个查询来查找所有最大面积的矩形。假设一个矩形由其左下角和右上角定义,并且 1/4 是 r,1/4 是 b,1/4 是 g,1/4 是 y。
所以结果应该是类似这样的:
x1 | y1 | x2 | y2 | area
-------------------------
0 1 6 9 58
1 2 4 7 58
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个计算面积的函数,然后查询该函数以获得最大的面积。
Make a function that calculates the area and then query the function to get the biggest one.