Dies beantwortet die ursprüngliche Version des Problems.
Sie scheinen Summen vergleichen zu wollen, nachdem Sie die Tabellen 2 und 3 aggregiert haben. Ich weiß nicht, was table1
ist für. Es scheint nichts zu tun.
Also:
select *
from (select partner_id, sum(quantity) as sum_quantity
from (select tdsl.*,
row_number() over (partition by t2.partner_id order by order_date) as seqnum
from trip_delivery_sales_lines tdsl
) tdsl
where seqnum <= 2
group by tdsl.partner_id
) tdsl left join
(select cl.partner_id, count(*) as leaf_cnt
from coupon_leaf cl
group by cl.partner_id
) cl
on cl.partner_id = tdsl.partner_id
where leaf_cnt is null or sum_quantity > leaf_cnt