2

我的表格网格中有一堆坐标,其模式id 为 x1,x2,y1,y2,如下所示:

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

网格表也有每个坐标元组的 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 以及每年以下 - 坐标元组 x1,x2,y1,y2 满足 >=60 次车祸的标准。

4

3 回答 3

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 只是有点难:(这最初是“我将如何检索在接下来的几年中发生超过 60 次崩溃的所有“方形网格”(x1、x2、y1、y2 的元组)......?”)

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“我将如何检索在以下年份(每年)发生超过 60 次崩溃的所有“方形网格”(x1、x2、y1、y2 的元组):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 子句可以使用......

于 2012-04-05T03:55:50.363 回答
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
于 2012-04-05T04:04:19.923 回答
1

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

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

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
于 2012-04-05T04:39:25.603 回答