mysql - 找出 2007 年撞车事故少于 1989 年的所有方形网格吗?

标签 mysql sql

我的 nycgrid 表具有以下架构 id,x1,x2,y1,y2。它看起来像下面的例子:

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

另一个表 nyccrash 包含有关 1989 年至 2007 年发生的车祸信息的元组(总共 12000 条记录)。第一个属性是crash_year,后面是事故类型、天气状况...等,最后是车祸发生的x_坐标y_坐标发生在纽约。

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

这是我之前在 stackoverflow 上提出的问题的扩展问题。

I am trying to find the square grids (Grid ID) such that they have less car crashes occurred in 2007 than in 1989. There are about 100 rows in the nycgrid table. I need only those rows that have had less accidents happened in the year in 2007 than in the year 1989.

And second part, which is probably slightly easier is how could I find the number of car crashes per each square grid (nycgrid.id)? In other words, how display how many crashes occurred on each grid id? Each crash has associated x and y coordinate. Each grid has a x1-x2-y1-y2 coordinates that make up a square.

最佳答案

RBarryYoung's answer 开头对于你的最后一个问题,我们只是使用 SUM/CASE 以年份为中心,然后比较值

SELECT ID
FROM

    (SELECT  
                grid.ID, 
                SUM(CASE WHEN yearCol = 1989 THEN 1 ELSE 0 END) CrashCount_1989,
                SUM(CASE WHEN yearCol = 2007 THEN 1 ELSE 0 END) CrashCount_2007
    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(1989, 2007)
    GROUP BY    grid.ID) t
WHERE CrashCount_2007 < CrashCount_1989

关于mysql - 找出 2007 年撞车事故少于 1989 年的所有方形网格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10036179/

相关文章:

Python 到 MySQL 数据库。 Errno 9 错误的文件描述符

mysql - 如果两个应用程序都具有多数据库架构,如何将不同应用程序的数据存储在同一个本地MySQL实例中?

mysql - 来自 GeoDjango PointField 的 Mysql 中无法读取数据

php - 简化多个嵌套的 MYSQL 选择查询

sql - 如何在 Transact SQL ( SQL Server 2008 ) 中实现这一点

mysql多对多删除级联留下孤儿

MYSQL 查询帮助 JOIN 表

mysql 触发器错误

MYSQL 等效于 MS-Access 函数 first() last()

mysql - IF 语句在此位置无效