管理xmlagg的空值

时间:2019-04-27 12:52:22

标签: xml oracle scala apache-spark aggregation

我对以下结构有疑问:

SELECT 'SELECT ' || col_list || ' from schema.table;' from( Select table_name,  rtrim(xmlagg(xml element(e,  Case when datatype in ('blob', 'timestamp') then null else column_name end , ', ').extract('//text()') order by c_id).getclobval(), ', ' ) col_list from all_tab_cols where schema ='schema' and table in ('t1', 't2') group by table_name)

当前两列,第四列和第六列为blob或时间戳记类型为时,这会给我输出:

SELECT ,  ,  third_col, , fifth_col, from schema.table;

如何修改查询,以便它提供:

SELECT third_col,  fifth_col from schema.table;

此结果选择语句进一步作为一行存储在spark数据帧中(我正在使用Scala)。因此,如果我们可以通过正则表达式修改查询或替换子字符串,那也可以解决问题,我也欢迎那些建议,但是如果我不必以这种方式破解并管理它,我将不胜感激。在查询方面本身。

1 个答案:

答案 0 :(得分:0)

我认为您过于复杂了;只是根本不包括这些列,只需在where子句中过滤掉它们即可:

...
  from all_tab_cols
  where owner = 'schema'
  and table_name in ('t1', 't2')
  and data_type != 'BLOB'
  and data_type not like 'TIMESTAMP%'
  group by table_name
)

然后您可以删除案例表达式。

您还可以简化一下:

select 'SELECT '
  || rtrim(xmlagg(xmlelement(e, column_name, ', ').extract('//text()') order by column_id).getclobval(), ', ' )
  || ' FROM ' || owner || '.' || table_name || ';'
from all_tab_cols
where owner = 'schema'
and table_name in ('t1', 't2')
and data_type != 'BLOB'
and data_type not like 'TIMESTAMP%'
group by owner, table_name;

db<>fiddle