I have an SQL table that looks like so, but there are hundreds of thousands of rows:
+------+---------------------+-----------+------------+--------------+----------+
| id | timestamp | lat | lon | country_code | city |
+------+---------------------+-----------+------------+--------------+----------+
| 2231 | 2013-09-22 14:58:32 | 28.179199 | 113.113602 | CN | Changsha |
| 2232 | 2013-09-22 14:58:32 | 28.179199 | 113.113602 | CN | Changsha |
| 2233 | 2013-09-22 14:58:32 | 41.792198 | 123.432800 | CN | Shenyang |
| 2234 | 2013-09-22 14:58:32 | 31.045601 | 121.399696 | CN | Shanghai |
| 2235 | 2013-09-22 14:58:32 | 45.750000 | 126.650002 | CN | Harbin |
| 2236 | 2013-09-22 14:58:32 | 39.928902 | 116.388298 | CN | Beijing |
| 2237 | 2013-09-22 14:58:32 | 26.061399 | 119.306099 | CN | Fuzhou |
| 2238 | 2013-09-22 14:58:32 | 26.583300 | 106.716698 | CN | Guiyang |
| 2239 | 2013-09-22 14:58:32 | 39.928902 | 116.388298 | CN | Beijing |
| 2240 | 2013-09-22 14:58:32 | 31.045601 | 121.399696 | CN | Shanghai |
+------+---------------------+-----------+------------+--------------+----------+
I need to query based on timestamp (intervals) and get all the records that fit within that interval, count the rows that have the same city (and add the lat/lon of any item, they will be all the same for the same group). Currently I just have a normal select
and group in my application code (as per below) but this is slow since it needs to send over a few hundred kb to the application.
(python code to aggregate)
sorted_events = sorted(result, key=itemgetter('city'), reverse=False)
for k, g in groupby(sorted_events, key=itemgetter('city')):
group = list(g)
first_item = group[0]
unique_city_item = {
"city" : first_item['city'],
"country_code" : first_item['cc'],
"lon" : first_item['lon'],
"lat" : first_item['lat'],
"number_of_items" : len(group)
}
It works the way I want it but it's slow. Is there a way to do this with an sql query directly? I get the following JSON output, I'd like something similar:
{
{
city: "Baotou",
lon: 109.822197,
country_code: "CN",
lat: 40.652199,
number_of_items: 288
},
{
city: "Beijing,",
lon: 116.388298,
country_code: "CN",
lat: 39.928902,
number_of_items: 47
}
}
Is this what you are looking for?
select city, lon, country_code, lat, count(*) as number_of_items
from table t
where timestamp between STARTTIMESTAMP and ENDTIMESTAMP
group by city, lon, country_code, lat;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.