[英]Using a query result to set table of next query
我有以下查询,返回两个不同表中基本唯一的行的最小值,最大值和用户总数。
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个以上的UNION块那样,我必须对mastertable
列出的所有表mastertable
,有没有一种方法可以使用查询来对mastertable
进行清理呢?
如果我在Java中执行此操作,则需要先从主表中获取ResultSet,然后再执行10个查询,然后自己对它们进行UNION
。 但是我宁愿我的数据库进行所有处理,并能够基于mastertable
的内容进行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不接受文字/值到标识符的动态转换。 因此,您需要先构建查询然后执行它。 您可以在客户端(似乎是Java)中执行此操作,也可以在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函数。 您可能是指now()
或CURRENT_DATE
..,具体取决于rt
的未公开类型。 我用now()
代替。
您很可能想在这里使用UNION ALL
而不是UNION
。
还修复了子查询缺少的别名,并做了一些小的简化。
使用EXECUTE
将其包装到一个plpgsql函数中。 这里有很多与SO紧密相关的示例,它们带有代码,链接和说明...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.