简体   繁体   English

Select 查询使用 json 格式值

[英]Select query using json format value

If customer first_name-'Monika', last_name='Awasthi'如果客户 first_name-'Monika',last_name='Awasthi'

Then I am using below query to return value in json format:然后我使用以下查询以 json 格式返回值:

SELECT * 
  FROM
  (
   SELECT JSON_ARRAYAGG(JSON_OBJECT('CODE' IS '1','VALUE' IS 'Monika'||' '||'Awasthi'))
     FROM DUAL 
  );
        

It is working fine & give below output:它工作正常并在 output 以下给出:

[{"CODE":"1","VALUE":"Monika Awasthi"}]

But I want one more value which should be reversed means output should be:但我想要一个应该反转的值意味着 output 应该是:

[{"CODE":"1","VALUE":"Monika Awasthi"},{"CODE":"2","VALUE":"Awasthi Monika"}]

Kindly give me some suggestions.请给我一些建议。 Thank You谢谢你

A simple logic through use of SQL (without using PL/SQL ) in order to generate code values as only be usable for two columns as in this case might be通过使用SQL (不使用PL/SQL )的简单逻辑,以便生成代码值,因为在这种情况下可能仅可用于两列

SELECT JSON_ARRAYAGG(
          JSON_OBJECT('CODE' IS tt.column_id,
                      'VALUE' IS CASE WHEN column_id=1 
                                      THEN name||' '||surname
                                      ELSE surname||' '||name
                                       END)
                    ) AS result
  FROM t
 CROSS JOIN (SELECT column_id FROM user_tab_cols WHERE table_name =  'T') tt

where t is a table which hold name and surname columns其中t是一个包含namesurname列的表

Demo 演示

More resilient solution might be provided through use of PL/SQL , even more columns exist within the data source such as通过使用PL/SQL可以提供更具弹性的解决方案,甚至更多列存在于数据源中,例如

DECLARE
 v_jso   VARCHAR2(4000);
 v_arr   OWA.VC_ARR; 
 v_arr_t JSON_ARRAY_T := JSON_ARRAY_T(); 
BEGIN
 FOR c IN ( SELECT column_id FROM user_tab_cols WHERE table_name = 'T' )
 LOOP
   SELECT 'JSON_OBJECT( ''CODE'' IS '||MAX(c.column_id)||',
                        ''VALUE'' IS '||LISTAGG(column_name,'||'' ''||') 
                                        WITHIN GROUP (ORDER BY ABS(column_id-c.column_id))
                                        ||' )'
     INTO v_arr(c.column_id)                                   
     FROM ( SELECT * FROM user_tab_cols WHERE table_name = 'T' );

   EXECUTE IMMEDIATE 'SELECT '||v_arr(c.column_id)||' FROM t' INTO v_jso;
   v_arr_t.APPEND(JSON_OBJECT_T(v_jso));
 END LOOP;

 DBMS_OUTPUT.PUT_LINE(v_arr_t.STRINGIFY);  
 
END;
/

Demo 演示

Another approach is to use a CTE to generate the two codes and values;另一种方法是使用 CTE 生成两个代码和值; your original version could be written to get the name data from a table or CTE:可以编写您的原始版本以从表或 CTE 中获取名称数据:

-- CTE for sample data
WITH cte (first_name, last_name) AS (
  SELECT 'Monika', 'Awasthi' FROM DUAL
)
-- query against CTE or table
SELECT JSON_ARRAYAGG(JSON_OBJECT('CODE' IS '1','VALUE' IS last_name ||' '|| first_name))
FROM cte;

And you could then extend that with a CTE that generates the value with the names in both orders:然后,您可以使用 CTE 扩展它,该 CTE 生成具有两个顺序中名称的值:

WITH cte1 (first_name, last_name) AS (
  SELECT 'Monika', 'Awasthi' FROM DUAL
),
cte2 (code, value) AS (
  SELECT 1 AS code, first_name || ' ' || last_name FROM cte1
  UNION ALL
  SELECT 2 AS code, last_name || ' ' || first_name FROM cte1
)
SELECT JSON_ARRAYAGG(JSON_OBJECT('CODE' IS code,'VALUE' IS value))
FROM cte2;

which gives:这使:

JSON_ARRAYAGG(JSON_OBJECT('CODE'ISCODE,'VALUE'ISVALUE))
-------------------------------------------------------------------------
[{"CODE":1,"VALUE":"Monika Awasthi"},{"CODE":2,"VALUE":"Awasthi Monika"}]

db<>fiddle db<>小提琴

As I explained in a comment under your question, I am not clear on how you define the CODE values for your JSON string (assuming you have more than one customer).正如我在您的问题下的评论中解释的那样,我不清楚您如何定义 JSON 字符串的CODE值(假设您有多个客户)。

Other than that, if you need to create a JSON array of objects from individual strings (as in your attempt), you probably need to use JSON_ARRAY rather than JSON_ARRAYAGG .除此之外,如果您需要从单个字符串创建一个 JSON 对象数组(如您的尝试),您可能需要使用JSON_ARRAY而不是JSON_ARRAYAGG Something like I show below.就像我在下面展示的那样。 Incidentally, I also don't know why you needed to SELECT * FROM (subquery) - the outer SELECT seems entirely unnecessary.顺便说一句,我也不知道你为什么需要SELECT * FROM (subquery) - 外部SELECT似乎完全没有必要。

So, if you don't actually aggregate over a table, but just need to build a JSON array from individual pieces:因此,如果您实际上并未在表上进行聚合,而只需要从各个部分构建 JSON 数组:

select json_array
       (
         json_object('CODE' is '1', 'VALUE' is first_name || ' ' || last_name ),
         json_object('CODE' is '2', 'VALUE' is last_name  || ' ' || first_name)
       ) as result
from   ( select 'Monika' as first_name, 'Awasthi' as last_name from dual )
;

RESULT                                                                        
------------------------------------------------------------------------------
[{"CODE":"1","VALUE":"Monika Awasthi"},{"CODE":"2","VALUE":"Awasthi Monika"}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM