2015-12-03 43 views
1

比方说,我在项目数据库JSON:串联列表

ROW1:

{"Id": "1", "Items": [{"Item": "Test Item", "Price": "$5.00"}, {"Item": "Test Item #2", "Price": "$15.00"}]} 

行2:

{"Id": "2", "Items": [{"Item": "Test Item #3", "Price": "$1.00"}, {"Item": "Test Item #1", "Price": "$4.00"}]} 

我如何获得此格式行( |是列分隔器):

1 | Test Item, Test Item #2 
2 | Test Item #3, Test Item #1 

回答

0
SELECT ID || '|' || ARRAY_TO_STRING(ARRAY_AGG(ITEMS), ', ') 
FROM 
    (
     SELECT T.J->>'Id' AS ID, json_array_elements((T.J->'Items')::json)->>'Item' AS ITEMS 
     FROM 
     (
      SELECT ('{"Id": "1", "Items": [{"Item": "Test Item", "Price": "$5.00"}, {"Item": "Test Item #2", "Price": "$15.00"}]}')::json AS J 
      UNION all 
      SELECT ('{"Id": "2", "Items": [{"Item": "Test Item #3", "Price": "$1.00"}, {"Item": "Test Item #1", "Price": "$4.00"}]}')::json AS J 
     ) T 
    ) T 
GROUP BY ID 
+0

不是寻找一个文字上的'|'但是现在,我将我所需要的。 – FrankerZ