SQL:指向矩形的点列表

时间:2010-08-01 11:00:51

标签: sql points regions

我有两张桌子:

regions <id>
points <region_id, x, y>

假设每个区域正好有4个点,而这些点描述了一个矩形 - 是否有一个SQL查询会给我带来这个视图:

rectangular_regions <region_id, x1, y1, x2, y2>

1 个答案:

答案 0 :(得分:10)

SELECT region_id, MIN(x) AS x1, MIN(y) AS y1, MAX(x) AS x2, MAX(y) AS y2 
FROM points 
GROUP BY region_id.