【问题标题】:How to execute and select statement within a select statement and do a string_agg or concat?如何在 select 语句中执行和 select 语句并执行 string_agg 或 concat?
【发布时间】:2019-11-11 12:45:02
【问题描述】:

我试图在一个选择中获得十几个表的概览,包括创建的 select-stmt 的数据。

我已经尝试过使用子选择,从表中选择执行(查询), 和一个选择:

combinedTable looks like:
|schema_name  | table_name | column_name|
----------------------------------------
|schema1      | table1     | column1    |
|schema1      | table2     | column1    |
|schema1      | table3     | column1    |
|schema2      | table1     | column1    |

而我正在努力做到这一点

combinedTable looks like:
|schema_name  | table_name | column_name|executedSQL           |
----------------------------------------------------------------
|schema1      | table1     | column1    |value1, value2, value3|
|schema1      | table2     | column1    |value1, value2, value3|
|schema1      | table3     | column1    |value1, value2, value3|
|schema2      | table1     | column1    |value1, value2, value3|

和“value1,value2,value3”都是嵌套select的结果


没有执行sql的表如下:

|schema_name  | table_name | column_name|NotExecutedSQL                              |
----------------------------------------------------------------
|schema1      | table1     | column1    |select string_agg(myColumn, ', ') from schema.table group by someColumn;|
|schema1      | table2     | column1    |select string_agg(myColumn, ', ') from schema.table group by someColumn;|
|schema1      | table3     | column1    |select string_agg(myColumn, ', ') from schema.table group by someColumn;|
|schema2      | table1     | column1    |select string_agg(myColumn, ', ') from schema.table group by someColumn;|

这个想法是有一个这样的选择语句

select execute t.a as foo 
from 
(
    select 
('select string_agg('||column_name||', '''||', '|| ''') from '||schema_name||'.'||table_name||' group by someColumn;') as a
from combined_table
) as t(a)

子查询的结果是:

"select string_agg(myColumn, ', ') from schema.table group by someColumn;"

这反过来确实给出了预期的结果,但只针对一个表,我希望为 combine_table 中的所有表执行该语句

-------
so expected/wished result would be:

|        foo         |
|--------------------|
|value1,value2,value3| <-- table1
|value1,value2       | <-- table2
|        ...         | <-- table ...

【问题讨论】:

  • 请向我们展示您的输入数据和整个预期输出。对我来说,完全不清楚你想要达到什么目的......
  • 我希望这更容易理解

标签: postgresql


【解决方案1】:

我相信您应该在表的列上使用 FOR LOOP。您可以使用 RETURN QUERY 每列返回一行。这是一个基本函数,它使用表作为参数。我在这里使用regclass 来避免不同模式中同名表之间的歧义。

CREATE OR REPLACE FUNCTION table_overview(tbl regclass)
  RETURNS TABLE (
    schema_name text,
    table_name  text,
    column_name text,
    vales       text
  ) AS $$
  DECLARE
    l_column_name text;
    l_schema_name text;
    l_table_name text;
  BEGIN
    -- Get the name of the schema and table from pg_class
    SELECT relnamespace::regnamespace::text, 
           relname::text INTO l_schema_name, l_table_name
      FROM pg_class
      WHERE oid = tbl;

    -- Loop over the columns
    FOR l_column_name IN 
      SELECT attname 
      FROM pg_attribute
      WHERE attisdropped = false
        AND attnum > 0
        AND attrelid = tbl
        ORDER BY attnum
    LOOP
      RETURN QUERY EXECUTE 
        FORMAT('SELECT %L, %L, %L, string_agg(%I::text, '','') FROM %s', 
           l_schema_name, l_table_name, l_column_name, l_column_name, tbl);

    END LOOP;
    RETURN;
  END;

$$ LANGUAGE PLPGSQL;

让我们测试一下:

CREATE TABLE public.test (a int, b int);
insert into test VALUES (1, 2), (3, 4), (5, 6);
CREATE TABLE public.test2 (c int, d int);
insert into test2 values (7, 8), (9, 10);
CREATE SCHEMA not_as_public;
CREATE TABLE not_as_public.test (e text);
INSERT INTO not_as_public.test VALUES ('a');


SELECT overview.* 
-- Use a values statement here to list the tables you want to query.  
FROM (VALUES ('test'::regclass), -- test is in public, already in our search_path, so we don't need to specify the schema
             ('test2'), -- same here
             ('not_as_public.test') -- this one is not in our search_path, so we specify the schema
     ) as v(tbl) 
CROSS JOIN LATERAL table_overview(tbl) as overview;
  schema_name  | table_name | column_name | vales
---------------+------------+-------------+-------
 public        | test       | a           | 1,3,5
 public        | test       | b           | 2,4,6
 public        | test2      | c           | 7,9
 public        | test2      | d           | 8,10
 not_as_public | test       | e           | a
(5 rows)


-- Alternatively, select the tables out of pg_class
SELECT overview.*
FROM pg_class
CROSS JOIN LATERAL table_overview(oid::regclass) as overview
WHERE relname like 'test%';
  schema_name  | table_name | column_name | vales
---------------+------------+-------------+-------
 public        | test       | a           | 1,3,5
 public        | test       | b           | 2,4,6
 not_as_public | test       | e           | a
 public        | test2      | c           | 7,9
 public        | test2      | d           | 8,10
(5 rows)

【讨论】:

  • 谢谢 :-) 我不太明白它是如何工作的,但确实如此,非常感谢
  • 如果您不确定它是如何工作的,请随时提出具体问题。但是,如果它对您有用,那么接受此答案会很有帮助,以便将问题正确标记为已回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 2013-04-09
  • 2019-03-13
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多