用postgre语法把[{"行业":100.0},{"技术",100.0},{"政 策",90.0}]解析成三列只保留有值的行
假设解析后的三列名分别为 industry, technology 和 policy,可以使用以下语句:
SELECT (json_array_elements('[{"行业":100.0},{"技术":100.0},{"政策":90.0}]'::json)->>'行业')::float AS industry, (json_array_elements('[{"行业":100.0},{"技术":100.0},{"政策":90.0}]'::json)->>'技术')::float AS technology, (json_array_elements('[{"行业":100.0},{"技术":100.0},{"政策":90.0}]'::json)->>'政策')::float AS policy WHERE (json_array_elements('[{"行业":100.0},{"技术":100.0},{"政策":90.0}]'::json)->>'行业') IS NOT NULL OR (json_array_elements('[{"行业":100.0},{"技术":100.0},{"政策":90.0}]'::json)->>'技术') IS NOT NULL OR (json_array_elements('[{"行业":100.0},{"技术":100.0},{"政策":90.0}]'::json)->>'政策') IS NOT NULL;
在上述语句中,使用 json_array_elements 函数将 JSON 数组拆分成多个 JSON 对象,然后通过 ->> 运算符和列名取出对应的值,并将其转换为浮点数类型。同时,使用 WHERE 子句过滤掉值为空的行。
原文地址: https://www.cveoy.top/t/topic/Aea 著作权归作者所有。请勿转载和采集!