此查询为我提供了几行以及来自usingtable1
的相关记录。我已将 table2 中的行限制为每行 10 行。table2
left join lateral
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
但是我怎样才能包含count(*)
table2 中与 table1 ( select count(*) from table2 where table1_id = t1.id
) 相关的所有记录的总数。由于我正在进行横向连接,我不确定如何添加这些结果。
我可以重用我已经在做的横向连接,还是我必须做一个单独的横向连接,因为第一个有 alimit 10
并且count(*)
不需要限制?查询应该是什么样子才能使它像这样工作?(我认为可能有一种方法可以使用第一个横向连接中的数组切片语法来做到这一点,但我认为这会很昂贵,因为它必须获取所有行才能获得它们的计数。)