2

我正在使用 flask_sqlalchemy 和 flask_restless 创建一个 API 端点来访问来自 PostgreSQL 数据库的数据。如何在 flask-restless 中构造以下 SQL 查询?

SELECT AVG("SpeedBand"), "Location"
FROM public.traffic_data
WHERE "RoadName"='ABC ROAD'
GROUP BY "Location"

这将返回属于 "ABC ROAD" 的所有坐标对以及每对坐标的平均速度:

   avg   | Location
 ------------------------
1)'1.52' | 'X1 Y1 X2 Y2'
2)'1.85' | 'X3 Y3 X4 Y4'
 ------------------------

目前,此查询:

http://127.0.0.1:5000/api/traffic_data?q={"filters":[{"name":"RoadName","op":"==","val":"ABC ROAD"}]}

返回属于“ABC ROAD”的所有数据和不同时间的所有速度数据的 JSON 字符串:

{
  "num_results": 26, 
  "objects": [
    {
      "DateTime": "2017-09-07T19:25:37.035325", 
      "LinkID": "109002906", 
      "Location": "X1 Y1 X2 Y2", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 3, 
      "id": 34094
    }, 
    {
      "DateTime": "2017-09-07T19:25:37.035325", 
      "LinkID": "109002907", 
      "Location": "X3 Y3 X4 Y4", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 1, 
      "id": 38003
    }, 
    {
      "DateTime": "2017-09-13T09:57:07.163900", 
      "LinkID": "109002906", 
      "Location": "X1 Y1 X2 Y2", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 1, 
      "id": 92874
    }, 
    {
      "DateTime": "2017-09-13T09:57:07.163900", 
      "LinkID": "109002907", 
      "Location": "X3 Y3 X4 Y4", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 1, 
      "id": 96783
    }, 
    {
      "DateTime": "2017-09-13T09:57:07.163900", 
      "LinkID": "109002906", 
      "Location": "X1 Y1 X2 Y2", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 1, 
      "id": 114755
    }, 
    {
      "DateTime": "2017-09-13T09:57:07.163900", 
      "LinkID": "109002907", 
      "Location": "X3 Y3 X4 Y4", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 2, 
      "id": 114756
    }, 
    {
      "DateTime": "2017-09-13T10:08:17.727949", 
      "LinkID": "109002906", 
      "Location": "X1 Y1 X2 Y2", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 2, 
      "id": 151654
    }, 
    {
      "DateTime": "2017-09-13T10:08:17.727949", 
      "LinkID": "109002907", 
      "Location": "X3 Y3 X4 Y4", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 2, 
      "id": 155563
    }, 
    {
      "DateTime": "2017-09-13T10:19:39.040191", 
      "LinkID": "109002906", 
      "Location": "X1 Y1 X2 Y2", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 2, 
      "id": 210434
    }, 
    {
      "DateTime": "2017-09-13T10:19:39.040191", 
      "LinkID": "109002907", 
      "Location": "X3 Y3 X4 Y4", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 2, 
      "id": 214343
    }, 
    {
      "DateTime": "2017-09-13T10:27:51.307349", 
      "LinkID": "109002906", 
      "Location": "X1 Y1 X2 Y2", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 2, 
      "id": 269214
    }, 
    {
      "DateTime": "2017-09-13T10:42:36.724985", 
      "LinkID": "109002906", 
      "Location": "X1 Y1 X2 Y2", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 2, 
      "id": 327994
    }, 
    {
      "DateTime": "2017-09-13T10:42:36.724985", 
      "LinkID": "109002907", 
      "Location": "X3 Y3 X4 Y4", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 2, 
      "id": 331903
    }, 
    {
      "DateTime": "2017-09-13T10:42:36.724985", 
      "LinkID": "109002906", 
      "Location": "X1 Y1 X2 Y2", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 1, 
      "id": 349875
    }, 
    {
      "DateTime": "2017-09-13T10:42:36.724985", 
      "LinkID": "109002907", 
      "Location": "X3 Y3 X4 Y4", 
      "RoadName": "ABC ROAD", 
      "SpeedBand": 2, 
      "id": 349876
    },

    . . .


  ], 
  "page": 1, 
  "total_pages": 1
}
4

1 回答 1

0

对于 group by,您需要按以下格式指定 feild。group_by 形式的对象列表:

{"field": <fieldname>}

这里 fieldname 是 group by 使用的列。

于 2017-09-21T02:17:57.223 回答