SQL Grid Ranges

随声附和 提交于 2019-12-12 01:56:40

问题


I have a list of customers with an x and y co-ordinate for each between -50 and 50 for each. I need to calculate the number of customers in each 10x10 grid square but don't know how I can group customers by a range in two columns. The Customer table has a customerID, customer_x and customer_y column. I've managed to get the result for a single column using

SELECT 10 * ( customer_x / 10 ) AS start_range, 10 * ( customer_x / 10 ) + 9 AS end_range, count(*) AS COUNT FROM t_customer GROUP BY customer_x / 10

I found this here, but can't work out how to get this working with a 2 dimensional range. I've started writing out a case for each grid-square using Select * from customer where (customer_x, customer_y) between (-50,-50) and (-50,-40) and so on, but there's 100 in total and I'm assuming there's a more efficient way? I'm using SQLite3. Any help would be really appreciated.

I forgot to add, it actually only needs to show the grid square with the highest number of customers, so there may be a far easier way of doing this?


回答1:


GROUP BY customer_x / 10, customer_y / 10


来源:https://stackoverflow.com/questions/41559264/sql-grid-ranges

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!