给定方形网格坐标,如何计算属于该范围的不同表格中的数据?

时间:2012-04-05 03:31:40

标签: mysql sql

我的表 grid 中有一堆坐标,其中包含以下架构 id,x1,x2,y1,y2 ,如下所示:

22,910000,920000,120000,130000
67,930000,940000,170000,180000
171,980000,990000,210000,220000

Grid 表也有每个坐标元组的ID。

在单独的表崩溃中我有关于汽车碰撞的信息,其中最后两个值是“ x_coordinate ”和“ y_coordinate '分别。

2007,2,9,4,1,1028977,202232
2004,1,1,1,4,1012600,214101
2003,1,9,1,1,958775,156149
1999,1,1,1,1,997349,175503

1 - 我如何计算每个方格的崩溃次数? (显示2列:网格ID和与之关联的崩溃次数)

2 - 如果走向另一个方向,我将如何检索在接下来的几年中有超过60次崩溃的所有“方格网格”(x1,x2,y1,y2的元组):2005,2006和2007年? (在HTML中,它看起来像一个包含3列的表:2005 | 2006 | 2007,每年下面 - 符合符合> = 60次车祸标准的元组x1,x2,y1,y2。

3 个答案:

答案 0 :(得分:4)

#1很简单: (这最初是“如何计算哪些车祸从网格表中落入x1,x2,y1,y2坐标的方格?”)

SELECT  DISTINCT
            grid.ID
FROM        crashes
INNER JOIN  grid    
            ON  crashes.x_coordinate BETWEEN grid.x1 AND grid.x2
            And crashes.y_coordinate BETWEEN grid.y1 AND grid.y2

#2只是有点困难: (这最初是“我将如何检索所有”方格网格(x1,x2,y1,y2的元组),在接下来的几年中有超过60次崩溃...?“)

SELECT  
            grid.ID, COUNT(*) AS CrashCount
FROM        crashes
INNER JOIN  grid    
            ON  crashes.x_coordinate BETWEEN grid.x1 AND grid.x2
            And crashes.y_coordinate BETWEEN grid.y1 AND grid.y2
WHERE       crashes.yearCol IN(2005, 2006, 2007)
GROUP BY    grid.ID
HAVING      COUNT(*) >= 60

修订问题的解决方案......

对于#1“我如何计算每个方格的崩溃次数?”,只是对原始#2的简化:

SELECT  
            grid.ID, COUNT(*) AS CrashCount
FROM        crashes
INNER JOIN  grid    
            ON  crashes.x_coordinate BETWEEN grid.x1 AND grid.x2
            And crashes.y_coordinate BETWEEN grid.y1 AND grid.y2
WHERE       crashes.yearCol IN(2005, 2006, 2007)
GROUP BY    grid.ID

对于#2“我将如何检索所有'正方形网格'(x1,x2,y1,y2的元组),其中有超过60次崩溃( 每个 )接下来的几年:2005,2006和2007年??

SELECT *
FROM
(
    SELECT  grid.ID,
            grid.x1, grid.x2, grid.y1, grid.y2,
            (   SELECT  COUNT(*)
                FROM    crashes
                WHERE   yearCol = 2005
                  And   crashes.x_coordinate BETWEEN grid.x1 AND grid.x2
                  And   crashes.y_coordinate BETWEEN grid.y1 AND grid.y2
            )   As year05,
            (   SELECT  COUNT(*)
                FROM    crashes
                WHERE   yearCol = 2006
                  And   crashes.x_coordinate BETWEEN grid.x1 AND grid.x2
                  And   crashes.y_coordinate BETWEEN grid.y1 AND grid.y2
            )   As year06,
            (   SELECT  COUNT(*)
                FROM    crashes
                WHERE   yearCol = 2007
                  And   crashes.x_coordinate BETWEEN grid.x1 AND grid.x2
                  And   crashes.y_coordinate BETWEEN grid.y1 AND grid.y2
            )   As year07,
    FROM    grid
) As gridSum
WHERE   year05  >= 60
  And   year06  >= 60
  And   year07  >= 60

这个在TSQL中更容易做,我们有WITH子句可以使用...

答案 1 :(得分:2)

这将使您在崩溃表中发生崩溃的GridId。我假设您的意思是下面提到的BETWEEN快捷方式而不是> =和< =

SELECT
     crashes.*
    ,(
        SELECT grid.id
        FROM grid
        WHERE
                crashes.x_coordinate BETWEEN grid.x1 AND grid.x2
            AND crashes.y_coordinate BETWEEN grid.y1 AND grid.y2
     ) AS GridId
FROM crashes

关于几年间网格崩溃的第二个问题,试试这个

SELECT GridId, SUM(CrashesInGrid) AS TotalCrashesInGrid
FROM
(   
    SELECT T.GridId, T.year, COUNT(*) AS CrashesInGrid
    FROM
    (
        SELECT
             *
            ,(
                SELECT grid.id
                FROM grid
                WHERE
                        crashes.x_coordinate BETWEEN grid.x1 AND grid.x2
                    AND crashes.y_coordinate BETWEEN grid.y1 AND grid.y2
             ) AS GridId
        FROM crashes
    ) AS T
) AS T2
WHERE T2.year >= 2005 AND T2.year <= 2007
GROUP BY T2.Year
HAVING SUM(CrashesInGrid) >= 60

答案 2 :(得分:1)

我无法比Barry更好地解决第一个问题:)

然而,这是否解决了第二个问题?如果没有,请告诉我。

SELECT id FROM (
  SELECT g.id, c.yearCol, COUNT(*) CrashCount FROM crashes c
  INNER JOIN grid g
  ON c.x_coordinate BETWEEN g.x1 and g.x2 AND c.y_coordinate BETWEEN g.y1 AND g.y2
  WHERE c.yearCol IN (2005, 2006, 2007)
  GROUP BY g.id, c.yearCol
  HAVING COUNT(*) >= 60
) final
GROUP BY id
HAVING COUNT(yearCol) = 3