使用现有的横向连接获取 postgres 中相关行的计数
Get count of related rows in postgres with existing lateral join
此查询使用 left join lateral
为我提供了来自 table1
的几行以及来自 table2
的相关记录。我已将 table2 中的行限制为每行 10 行。
select t1.id, array_agg(t2.column1)
from table1 t1
left join lateral (select * from table2 where table1_id = t1.id order by column2 limit 10) t2 on true
where t1.other = other_value
group by t1.id
但是我怎样才能包括 table2 中与 table1 (select count(*) from table2 where table1_id = t1.id
) 相关的所有记录的总数 count(*)
。由于我正在进行横向连接,因此我不确定如何添加这些结果。
我可以重复使用我已经在做的横向连接,还是我必须做一个单独的横向连接,因为第一个有 limit 10
而 count(*)
不需要限制?查询应该是什么样子才能像这样工作? (我认为可能有一种方法可以使用来自第一个横向连接的数组切片语法来完成它,但我认为这会很昂贵,因为它必须获取所有行才能获得它们的数量。)
你不妨为此使用 window 函数:
select t1.id, array_agg(t2.column1)
from table1 t1 left join
(select t2.*, count(*) over (partition by table1_id) as cnt,
row_number() over (partition by table1_id order by column2) as seqnum
from table2
) t2
on t2.table1_id = t1.id and sequm <= 10
where t1.other = other_value
group by t1.id;
您可以单独执行此操作 lateral join
。
编辑:
使用单独的横向连接:
select t1.id, array_agg(t2.column1), t2c.cnt
from table1 t1 left join lateral
(select *
from table2
where table1_id = t1.id
order by column2
limit 10
) t2
on true left join lateral
(select count(*) as cnt
from table2
where table1_id = t1.id
) t2c
on true
where t1.other = other_value
group by t1.id, t2.cnt;
或者在外部查询中没有聚合的单个横向连接:
select t1.id, t2.column1s, t2.cnt
from table1 t1 left join lateral
(select array_agg(t2.column1) as column1, max(cnt) as cnt
from (select t2.*,
row_number() over (order by column2 desc) as seqnum,
count(*) over () as cnt
from table2
where table1_id = t1.id
) t2
where seqnum <= 10
) t2
on true left join
where t1.other = other_value;
这可能是最好的方法。
此查询使用 left join lateral
为我提供了来自 table1
的几行以及来自 table2
的相关记录。我已将 table2 中的行限制为每行 10 行。
select t1.id, array_agg(t2.column1)
from table1 t1
left join lateral (select * from table2 where table1_id = t1.id order by column2 limit 10) t2 on true
where t1.other = other_value
group by t1.id
但是我怎样才能包括 table2 中与 table1 (select count(*) from table2 where table1_id = t1.id
) 相关的所有记录的总数 count(*)
。由于我正在进行横向连接,因此我不确定如何添加这些结果。
我可以重复使用我已经在做的横向连接,还是我必须做一个单独的横向连接,因为第一个有 limit 10
而 count(*)
不需要限制?查询应该是什么样子才能像这样工作? (我认为可能有一种方法可以使用来自第一个横向连接的数组切片语法来完成它,但我认为这会很昂贵,因为它必须获取所有行才能获得它们的数量。)
你不妨为此使用 window 函数:
select t1.id, array_agg(t2.column1)
from table1 t1 left join
(select t2.*, count(*) over (partition by table1_id) as cnt,
row_number() over (partition by table1_id order by column2) as seqnum
from table2
) t2
on t2.table1_id = t1.id and sequm <= 10
where t1.other = other_value
group by t1.id;
您可以单独执行此操作 lateral join
。
编辑:
使用单独的横向连接:
select t1.id, array_agg(t2.column1), t2c.cnt
from table1 t1 left join lateral
(select *
from table2
where table1_id = t1.id
order by column2
limit 10
) t2
on true left join lateral
(select count(*) as cnt
from table2
where table1_id = t1.id
) t2c
on true
where t1.other = other_value
group by t1.id, t2.cnt;
或者在外部查询中没有聚合的单个横向连接:
select t1.id, t2.column1s, t2.cnt
from table1 t1 left join lateral
(select array_agg(t2.column1) as column1, max(cnt) as cnt
from (select t2.*,
row_number() over (order by column2 desc) as seqnum,
count(*) over () as cnt
from table2
where table1_id = t1.id
) t2
where seqnum <= 10
) t2
on true left join
where t1.other = other_value;
这可能是最好的方法。