如何创建一个包含JSON的列,该JSON的名称是从另一个表中的列的值定义的?

时间:2018-06-27 14:38:22

标签: sql json teradata

我有一个源表,其中包含VARCHAR格式的数据,如下例所示。 我想将数据以JSON格式插入到另一个表中(结果列本身可以是JSON或VARCHAR类型)。

  • 对于每个ID,至少有1个JSONName / JSONValue对。
  • 但是每个ID的JSONName / JSONValue对的种类和数量都不相同。
  • 每个ID最多可以有50个JSONName / JSONValue对。
  • 在ResultJSON列的值中,对的顺序无关紧要。

SourceTable:
____________________________
| Id | JSONName | JSONValue |
|____|__________|___________|
| 1  | Name     | John      |
| 2  | Name     | Henry     |
| 2  | Age      | 32        |
| 3  | Age      | 56        |
| 3  | Location | US        |
| 4  | Age      | 24        |
| 4  | Name     | Andrew    |
| 4  | Location |           |

我想要什么:

Expected ResultTable:
____________________________________________________
| Id |               ResultJSON                     |
|____|______________________________________________|
| 1  | {"Name":"John"}                              |
| 2  | {"Name":"Henry","Age":"32"}                  |
| 3  | {"Age":"56", "Location":"US"}                |
| 4  | {"Age":"24","Name":"Andrew","Location":null} |

我当前查询得到的结果:

Wrong resultTable:
_______________________________________________________________________________________________________________________________
| Id |               ResultJSON                                                                                                |
|____|_________________________________________________________________________________________________________________________|
| 1  | [{"JSONName":"Name","JSONValue":"John"}]                                                                                |
| 2  | [{"JSONName":"Name","JSONValue":"Henry"},{"JSONName":"Age","JSONValue":"32"}]                                           |
| 3  | [{"JSONName":"Age","JSONValue":"56"},{"JSONName":"Location","JSONValue":"US"}]                                          |
| 4  | [{"JSONName":"Age","JSONValue":"24"},{"JSONName":"Name","JSONValue":"Andrew"},{"JSONName":"Location","JSONValue":null}] |

当前查询:

INSERT INTO ResultTable
(
     Id
    ,ResultJSON
)
SELECT
     SourceTable.Id
    ,JSON_AGG(SourceTable.JSONName,SourceTable.JSONValue)
FROM SourceTable
INNER JOIN OtherTable ON SourceTable.Id=OtherTable.Id

是否可以使用Teradata JSON函数来实现?如果没有,那么最优化的查询将是什么?

2 个答案:

答案 0 :(得分:0)

这是我最后得到的查询:

INSERT INTO DB.RESULT_TABLE
(
     ResultId
    ,ResultJSON
)
WITH RECURSIVE MergedTable (Id, mergedList, rnk)
AS
(
    SELECT
         Id
        ,TRIM('"' || JSONName ||'":'|| COALESCE('"' || JSONValue || '"','null')) AS mergedList
        ,rnk
    FROM DB.SOURCE_TABLE
    WHERE rnk = 1

    UNION ALL

    SELECT
         SourceTable.Id
        ,MergedTable.mergedList || ',' || TRIM('"' || SourceTable.JSONName ||'":' || COALESCE('"' || SourceTable.JSONValue || '"','null')) AS mergedList
        ,SourceTable.rnk
    FROM DB.SOURCE_TABLE SourceTable
    INNER JOIN MergedTable MergedTable
        ON MergedTable.rnk + 1 = SourceTable.rnk
        AND  SourceTable.Id = MergedTable.Id
)
SELECT
     MergedTable.Id                       AS ResultId
    ,'{' || MergedTable.mergedList || '}' AS ResultJSON
FROM MergedTable
QUALIFY RANK() OVER (PARTITION BY ResultId ORDER BY rnk DESC) = 1
;

答案 1 :(得分:0)

您可以使用RegEx删除不需要的部分:

SELECT
   SourceTable.Id
  ,RegExp_Replace(Cast(Json_Agg(SourceTable.JSONName AS "#A",SourceTable.JSONValue AS "#B") AS VARCHAR(32000)), '"#A":|,"#B"|^\[|\]$|}(?=,{")|(?<="},){')
FROM SourceTable
GROUP BY 1

RegEx删除了所有以下内容:

  • "#A":
  • ,"#B"
  • 领先的[
  • 尾随]
  • }(如果后面紧跟,{"
  • {(如果紧随"},

编辑:

根据评论,此RegEx留下了多余的开括号。看来效果更好:

'"#A":|,"#B"|^[|]$|}(?=,)|(?<=,){'