sql - 如何从geojson中删除少于4个值的坐标
问题描述
正如标题所说,我正在对存储在 bigquery 中的共享单车数据进行查询,我能够提取数据并以正确的顺序排列以显示在路径图中。在数据中,只有开始和结束经纬度协调,或者有时只开始经纬度,我如何删除少于 4 点的任何东西?这是代码,我也仅限于选择
SELECT
routeID ,
json_extract(st_asgeojson(st_makeline( array_agg(st_geogpoint(locs.lon, locs.lat) order by locs.date))),'$.coordinates') as geo
FROM
howardcounty.routebatches
where unlockedAt between {{start_date}} and {{end_date}}
cross join UNNEST(locations) as locs
GROUP BY routeID
order by routeID
limit 10
解决方案
要在 a 之后应用条件group by
,请使用 a having
。对于一个简单的条件——路线是否至少有两个数据集?-- 可以使用这个查询:
With dummy as (
Select 1 as routeID, [struct(current_timestamp() as date, 1 as lon, 2 as lat),struct(current_timestamp() as date, 3 as lon, 4 as lat)] as locations
Union all select 2 as routeID, [struct(current_timestamp() as date, 10 as lon, 20 as lat)]
)
SELECT
routeID , count(locs.date) as amountcoord,
json_extract(st_asgeojson(st_makeline( array_agg(st_geogpoint(locs.lon, locs.lat) order by locs.date))),'$.coordinates') as geo
FROM
#howardcounty.routebatches
dummy
#where unlockedAt between {{start_date}} and {{end_date}}
cross join UNNEST(locations) as locs
GROUP BY routeID
having count(locs.date)>1
order by routeID
limit 10
对于更复杂的,嵌套select
可能会完成这项工作:
Select *
from (
--- your code ---
) where length(geo)-length(replace(geo,"]","")) > 1+4
JSON 在您的代码中转换为字符串。如果]
对 JSON 的结尾进行计数并减去 1,则计算内部数组。
推荐阅读
- ios - 无法构建 - expo buid:ios
- arrays - Typescript - 从联合或数组派生对象类型
- google-sheets - 数组参数 countifs 的大小不同 - Google 表格
- python - Pandas / Python - Merge dataframes where the key is located in 2 sub-strings
- javascript - 在 iOS 上一个接一个地播放视频,无需用户交互
- python-3.x - SQL 注入预防 Python 3 语法 - “.format” 替换
- python - 如何获取每周 Google Fit Rest API 数据
- c++ - for循环中的Lambda - 静态变量
- javascript - “对象作为 React 子项无效”和 linter
- java - 如何为单个浮点值创建 ONNXTensor?