我需要在下面的MySQL SELECT语句中添加一个rank列:
SELECT cbn.neighborhoods ASneighborhoods
, COUNT(*) ASoffenses
, na.neighborhoods, TRUNCATE( ((na.neighborhood_area_in_sq_meters /1000) * 0.000386102159), 2 ) ASsq_miles
,
(COUNT(*) / ((na.neighborhood_area_in_sq_meters /1000) * 0.000386102159)) ASoffenses_per_sq_mile
FROM wp_crime_by_neighborhood AScbn
, wp_neighborhood_area ASna
WHERE cbn.offense = 'AWD' AND cbn.neighborhoods = na.neighborhoods GROUP BY cbn.neighborhoods ORDER BY offenses_per_sq_mile DESC;
表格如下:
表名:wp_crime_by_neighborhood
nid bigint(11)
ccn int(10)
邻里varchar(50)
reportdatetime datetime
结果tinyint(1)
纬度小数(11,9)
经度十进制(10,8)
进攻varchar(30)表名:wp_neighborhood_area neighborhood_index smallint(3)
邻里varchar(50)
neighborhood_area_in_sq_meters int(10)
我想基于这个位排名:(COUNT(*)/((na.neighborhood_area_in_sq_meters / 1000)* 0.000386102159))AS offenses_per_sq_mile。
Stack Overflow海报,a1ex07,帮助我回到另一个排名问题,并提供了这个解决方案:
SELECT t1.name, (SELECT COUNT(*) FROM table_1 t2 WHERE t2.score >
t1.score) +1
AS rnk
FROM table_1 t1
谢谢你,a1ex07!在这种情况下,我不确定如何应用上述解决方案。在过去,我有一个简单的分数案例,这有点复杂。有什么建议吗?