sql - 拆分列数据并过滤它们
问题描述
使用 SQL 语句,我想按某一列中的某个坐标过滤表。
我想将它们拆分为经纬度,然后过滤每个值。如何在 SQL 中完成。
id pid code class sclass name coordinates
24079 4273753 5122 Roads residential Apoteksgatan [
[
[
58.26574,
12.9477909
],
[
58.2643948,
12.9490654
],
[
58.2640484,
12.9494174
],
[
58.2638978,
12.9498716
],
[
58.2637401,
12.9505464
],
[
58.2634622,
12.9518316
]
]
]```
解决方案
如果您是 SQL Server 2016 版或更高版本,则可以使用OPENJSON()
DECLARE @testdata TABLE
(
[id] INT
, [pid] BIGINT
, [code] INT
, [class] NVARCHAR(100)
, [sclass] NVARCHAR(100)
, [name] NVARCHAR(100)
, [coordinates] NVARCHAR(MAX)
);
INSERT INTO @testdata (
[id]
, [pid]
, [code]
, [class]
, [sclass]
, [name]
, [coordinates]
)
VALUES ( 24079
, 4273753
, 5122
, 'Roads'
, 'residential'
, 'Apoteksgatan'
, '[ [ [ 58.26574, 12.9477909 ], [ 58.2643948, 12.9490654 ], [ 58.2640484, 12.9494174 ], [ 58.2638978, 12.9498716 ], [ 58.2637401, 12.9505464 ], [ 58.2634622, 12.9518316 ] ] ]' );
SELECT [a].[id]
, [d].[Key]
, [d].[Value]
FROM @testdata [a]
CROSS APPLY OPENJSON([a].[coordinates]) [b]
CROSS APPLY OPENJSON([b].[Value]) [c]
CROSS APPLY OPENJSON([c].[Value]) [d];
这给了你以下结果
id Key Value
----------- --- -----------
24079 0 58.26574
24079 1 12.9477909
24079 0 58.2643948
24079 1 12.9490654
24079 0 58.2640484
24079 1 12.9494174
24079 0 58.2638978
24079 1 12.9498716
24079 0 58.2637401
24079 1 12.9505464
24079 0 58.2634622
24079 1 12.9518316
推荐阅读
- postgresql - 码头工人 | Postgres 数据库未初始化且未指定超级用户密码
- python - 保存启用宏的电子表格时出现 pywin/win32com 文件访问错误
- java - Spring Security - REST API 的授权
- python - Python:如果使用正则表达式从另一个字符串中提取子字符串
- php - Wordpress+ACF - 通过转发器字段中的多选选项进行过滤
- search-engine - 将 TF-IDF 值存储在倒排索引中
- apache - 现有连接被远程主机强行关闭(net::ERR_HTTP2_PROTOCOL_ERROR)
- php - Nginx - 单一配置中的 Laravel 和 Reactjs
- android - 如果需要,使用多行以编程方式在 Layout 中添加多个自定义视图
- jsonata - jsonata 按“外部”属性过滤