Joining two tables based on two Aggregate columns

aggregatejoin;oracle

I have a following problem as in the example. I have to join two tables based on the aggregated columns. These are the only way to combine these two tables. Is it possible to do this? I have tried Having by – procedures, without any success.

Select a.customerID,
sum(a.Items) as Amount a
sum(b.Items) as Amount b
From Orders a

Join Table B
   on SUM(a.Items) = SUM(b.Items) 
Group By a.CustomerID

Best Answer

Something like this:

with 
ta as (select a.customerid, sum(a.items) amount_a
       from orders a
       group by a.customerid
      ),
tb as (select sum(b.items) amount_b
       from tableb b
      )
select ta.customerid, ta.amount_a, tb.amount_b
from ta join tb on ta.amount_a = tb.amount_b