删除输出中的括号

时间:2018-12-10 16:01:33

标签: sql postgresql

除了输出中没有括号外,是否有其他方法可以做到这一点?接受输出的API无法处理它们。

select a,b,c,d,e from (select n_dead_tup from pg_stat_user_tables where relname = 'table1') as a, 
(select n_dead_tup from pg_stat_user_tables where relname = 'table2') as b, 
(select n_dead_tup from pg_stat_user_tables where relname = 'table3') as c, 
(select n_dead_tup from pg_stat_user_tables where relname = 'table4') as d, 
(select n_dead_tup from pg_stat_user_tables where relname = 'table5') as e;

此输出是这样的:

 a        |     b     |    c     |     d     |     e     
----------+-----------+----------+-----------+-----------
(7781834) | (7781834) | (483464) | (1426296) | (5452406)

2 个答案:

答案 0 :(得分:2)

之所以会这样,是因为您是通过表名而不是列来选择 tuples 。如果您选择a.n_dead_tup,则会看到此信息。如果愿意,您可以对所有表重复此操作,但我更喜欢以下解决方案。

我建议一个简单的group by

select relname, n_dead_tup
from pg_stat_user_tables
where relname in ('table1', 'table2', 'table3', 'table4', 'table5');

或条件聚合:

select max(n_dead_tup) filter (relname = 'table1') as table1,
       max(n_dead_tup) filter (relname = 'table2') as table2,
       max(n_dead_tup) filter (relname = 'table3') as table3,
       max(n_dead_tup) filter (relname = 'table4') as table4,
       max(n_dead_tup) filter (relname = 'table5') as table5
from pg_stat_user_tables
where relname in ('table1', 'table2', 'table3', 'table4', 'table5') 

答案 1 :(得分:0)

正则表达式替换可能是最简单的方法:

regexp_replace(a, '\(|\)', '', 'g');

您需要在其中放置左或右paren的管道,因此,您需要使用斜杠对parense进行转义。 g是一个标志,用于从字符串中删除所有示例