Sql 使用查询结果设置下一个查询的表

Sql 使用查询结果设置下一个查询的表,sql,postgresql,union,plpgsql,dynamic-sql,Sql,Postgresql,Union,Plpgsql,Dynamic Sql,下面的查询返回基本上唯一的行的最小值、最大值和用户总数 在两张不同的桌子上 select sum(user_count) as user_count_sum, sum(min_count) as min_count_sum, sum(max_count) as max_count_sum from( select max(case when di = 'i' then di_count end) as user_count, min(di_count) as min_count

下面的查询返回基本上唯一的行的最小值、最大值和用户总数 在两张不同的桌子上

select sum(user_count) as user_count_sum, sum(min_count) as min_count_sum, 
sum(max_count) as max_count_sum
from(
select max(case when di = 'i' then di_count end) as user_count, 
       min(di_count) as min_count,
       max(di_count) as max_count
from (
    select di, 
           count(distinct rt) as di_count
    from gpstablev2 
    where rt >  GETDATE() - '1 day'::INTERVAL
    group by di
)
UNION
select max(case when di = 'i' then di_count end) as user_count, 
       min(di_count) as min_count,
       max(di_count) as max_count
from (
    select di, 
           count(distinct rt) as di_count
    from powertablev2 
    where rt >  GETDATE() - '1 day'::INTERVAL
    group by di
)
)
现在我还有一个主表,如果我运行下面的查询,它将返回下面的

select table from mastertable;

gpstablev2
powertablev2
...(more table names)
我不需要再粘贴9个并集块,比如我必须对mastertable中列出的所有表求和的并集块,有什么方法可以使用对mastertable的查询来清理它吗

如果我用java做这个,我会首先从主表中获取我的结果集,然后自己做10个查询并合并它们。但我宁愿让我的数据库做所有的处理,并且能够根据mastertable的内容进行更新

编辑: 根据反馈,我正在尝试一个动态查询,并且已经完成了这一步,但是我仍然无法将其余的部分组合在一起

CREATE OR REPLACE FUNCTION get_sums()
RETURNS TABLE(user_count_sum bigint, min_count_sum bigint, max_count_sum bigint)
$BODY$
BEGIN

RETURN QUERY EXECUTE "
SELECT $$SELECT sum(user_count) AS user_count_sum
      ,sum(min_count) AS min_count_sum
      ,sum(max_count) AS max_count_sum
FROM  (
   SELECT max(case when di = 'id' then di_count end) AS user_count
         ,min(di_count) AS min_count
         ,max(di_count) AS max_count
   FROM  ($$
||
string_agg(format($$
      (SELECT di, count(distinct rt) AS di_count
       FROM   %I
       WHERE  rt >  now() - interval '1 day'
       GROUP  BY 1)$$, tbl)
      ,'

      UNION ALL')
|| '
      ) sub1
   ) sub2'
FROM   mastertable;
INTO results_var"

END;
$BODY$
LANGUAGE plpqsql;
为此,您需要动态SQL。SQL不接受将文字/值动态转换为标识符。因此,您需要首先构建查询,然后执行它。您可以在您的客户机中完成这项工作,或者在Postgres中完成这项工作,这通常是最快的:

SELECT $$SELECT sum(user_count) AS user_count_sum
      ,sum(min_count)  AS min_count_sum
      ,sum(max_count)  AS max_count_sum
FROM  (
   ($$
||
string_agg(format($$
   SELECT max(case when di = 'i' then di_count end) AS user_count
         ,min(di_count) AS min_count
         ,max(di_count) AS max_count
   FROM  (
      SELECT di, count(distinct rt) AS di_count
      FROM   %I
      WHERE  rt >  now() - interval '1 day'
      GROUP  BY 1
      ) t
   )$$, tbl)
      ,'

   UNION ALL
   (')
|| '
   ) sub'
FROM   mastertable;
生成查询字符串:

SELECT sum(user_count) AS user_count_sum
      ,sum(min_count)  AS min_count_sum
      ,sum(max_count)  AS max_count_sum
FROM  (
   (
   SELECT max(case when di = 'i' then di_count end) AS user_count
         ,min(di_count) AS min_count
         ,max(di_count) AS max_count
   FROM  (
      SELECT di, count(distinct rt) AS di_count
      FROM   gpstablev2
      WHERE  rt >  now() - interval '1 day'
      GROUP  BY 1
      ) t
   )

   UNION ALL
   (
   SELECT max(case when di = 'i' then di_count end) AS user_count
         ,min(di_count) AS min_count
         ,max(di_count) AS max_count
   FROM  (
      SELECT di, count(distinct rt) AS di_count
      FROM   powertablev2
      WHERE  rt >  now() - interval '1 day'
      GROUP  BY 1
      ) t
   )

   UNION ALL
   ...

   ) sub;
GETDATE不是有效的Postgres函数。你可能是指现在或现在的约会。。取决于未公开的rt类型。我现在使用

您很可能希望在这里使用UNION ALL而不是UNION

还修复了子查询缺少的别名,并进行了一些小的简化


使用EXECUTE将其包装到plpgsql函数中。有很多代码、链接和解释…

您可以在PL/pgSQL函数中使用动态SQL google for EXECUTE来实现这一点。我只想了解一下如何执行动态SQL。试图从此示例中学习的类型定义丢失。应该是:返回TABLEuser\u count\u sum bigint、min\u count\u sum bigint、max\u count\u sum bigint。现在快速浏览一下,剩下的看起来不错。您的查询与正在讨论的查询不同。您需要将最大/最小聚合移动到每个子查询。