我正在使用flask_sqlalchemy和flask_restless創建一個API端點來訪問PostgreSQL數據庫中的數據。我如何構建以下SQL查詢在瓶子裏不安?如何構建一個「group_by」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
}
您是否可以不定義自己的端點來準確運行該SQL? –
我可以在SQLAlchemy中寫入相同的查詢結果= session.query(func.avg(TrafficData.SpeedBand),TrafficData.Location).group_by(TrafficData.Location).filter(TrafficData.RoadName =='ABC ROAD') .all()' 但我該如何將它寫入端點? [鏈接](https://flask-restless.readthedocs.io/en/stable/customizing.html) –
你可以寫入一個方法... https://flask-restless.readthedocs.io/en/ stable/customizing.html#custom-queries –