【问题标题】:getting error while aggregating a huge string with xmlagg使用 xmlagg 聚合一个巨大的字符串时出错
【发布时间】:2019-05-11 13:20:33
【问题描述】:

我正在尝试使用 xmlagg 聚合字符串,但我遇到了错误。这是 xmlagg

select  
            apex_string.format(t1.col_heading, null, null, 1, 2, null)  
          || rtrim(xmlagg(XMLELEMENT(e,apex_string.format(t2.col_heading, null, null, da.n_service, case da.gid when 15 then 3 else 1 end, coalesce(da.service_type, 'Grand Total')),'').EXTRACT('//text()') order by da.c ).GetClobVal(),',')
        , min(da.gid)  
      from  
          data_aggs da  
            cross join std_template t1  
            cross join std_template t2  
      where  
          da.balance_type is null  
      and da.gid in (11, 15)  
      group by  
          t1.col_heading

当我运行这个时,我发现下面的错误

[Error] Execution (132: 14): ORA-01790: expression must have same datatype as corresponding expression

【问题讨论】:

  • DA.GID 的数据类型是什么?
  • 它的字符类型
  • 然后 - 在 CASE 中 - 将其与字符串而不是数字进行比较
  • 你能举个例子吗
  • 当然;我发了,看看希望它会有所帮助。

标签: sql oracle string-aggregation


【解决方案1】:

这就是我的意思——如果DA.GID 的数据类型是VAR(CHAR)2——你应该将它与字符串进行比较,而不是数字。注意第 4、5 和 14 行。

SQL> select     apex_string.format(t1.col_heading, null, null, 1, 2, null)
  2               || rtrim(xmlagg(xmlelement
  3                    (e, apex_string.format(t2.col_heading, null, null, da.n_service,
  4                          case da.gid when '15' then '3'       --> here
  5                                      else '1'                 --> here
  6                          end,
  7                          coalesce(da.service_type, 'Grand Total')), '').extract('//text()')
  8                          order by da.c ).getclobval(), ','),
  9             min(da.gid)
 10  from       data_aggs da
 11  cross join std_template t1
 12  cross join std_template t2
 13  where      da.balance_type is null
 14  and        da.gid in ( '11', '15')                            --> here
 15  group by   t1.col_heading
 16  ;

[编辑]

TO_NUMBER 应用到CASE 中的DA.GID,但在第14 行保持原样

  4                          case to_number(da.gid) when 15 then 3       --> here
  5                                      else 1                          --> here
  6                          end,

 14  and        da.gid in ('11', '15')                                   --> here

【讨论】:

  • 现在得到[Error] Execution (132: 125): ORA-00932: inconsistent datatypes: expected NUMBER got CHAR
  • 代码中的第 132 行是哪一行?如果预期为 NUMBER,则可能相反(但仅当 DA.GID 仅包含数字时,尽管其数据类型为 CHAR。我编辑了该消息,看看是否有帮助。
  • || rtrim(xmlagg(xmlelement(e, apex_string.format(t2.col_heading, null, null, da.n_service,case to_number(da.gid) when 15 then 3 else 1 end, coalesce(da.service_type, 'Grand Total')), '').extract('//text()') 这是第 132 行,我已根据您更新的答案进行了更改,但现在错误为 [Error] Execution (132: 19): ORA-01790: expression must have same datatype as corresponding expression
  • 呸!我们又回到了最初的状态,没有任何改善:(
  • 是的!我已经尝试了你的建议,但我真的不知道为什么会发生这种情况
【解决方案2】:

这是我迄今为止所期待的

select  
          xmlconcat(  
              xmlparse(content apex_string.format(t1.col_heading, null, null, 1, 2, null))  
            , xmlagg(xmlparse(content apex_string.format(t2.col_heading, null, null, da.n_service, case da.gid when 15 then 3 else 1 end, coalesce(da.service_type, 'Grand Total'))) order by da.c))  
        , min(da.gid)  
      from  
          data_aggs da  
            cross join std_template t1  
            cross join std_template t2  
      where  
          da.balance_type is null  
      and da.gid in (11, 15)  
      group by  
          t1.col_heading

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2012-08-06
    • 2018-01-02
    • 2023-03-25
    • 2019-02-14
    相关资源
    最近更新 更多