我的表中有一个列,我想将其展平/扩展为列,以便可以从中进行转换
{"name a":1,"name b":1,"name c":1,"name d":1,"name e":1}
对此
|name a |name b|name c|name d |name e|
| 1 |1 |1 |1 |1 |
每个都有自己的列
答案 0 :(得分:0)
我找不到执行此操作的动态方法。无论您是扁平化然后重新备份列还是在下面以这种方式进行,您都需要引用列名。可能存在一种在存储过程中动态执行此操作或以Python或其他语言动态创建SQL的方法:
-- Creating a Mock table to query
WITH json_table AS (
SELECT TO_VARIANT(PARSE_JSON('{"name a":1,"name b":1,"name c":1,"name d":1,"name e":1}')) AS json_field
UNION ALL
SELECT TO_VARIANT(PARSE_JSON('{"name a":2,"name b":2,"name c":2,"name d":2,"name e":2}')) AS json_field
)
SELECT json_field:"name a" AS "name a"
,json_field:"name b" AS "name b"
,json_field:"name c" AS "name c"
,json_field:"name d" AS "name d"
,json_field:"name e" AS "name e"
FROM json_table j
;
答案 1 :(得分:0)
创建或替换表test222(src变体)
为
选择parse_json(column1)作为src
from values('{“ name a”:1,“ name b”:1,“ name c”:1,“ name d”:1,“ name e”:1}')
选择src:“名称a”作为“名称a”,src:“名称b”作为“名称b”,src:“名称c”作为“名称c”,src:“名称d”作为“名称d” “,src:” name e“作为test222中的” name e“