2010-08-01 6 views
5

J'ai deux tables:SQL: liste des points rectangle

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

En supposant il y a exactement 4 points par région, et ces points décrivent un rectangle - est-il une requête SQL qui va me mettre cette vue:

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

?

Répondre

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.