0

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
2
  • 1
    I don't know if I should give you the answer or just a hint: use 2x CTEs (the WITH clause) - one for each table with sum() Sep 25 '20 at 18:28
  • Thank you for the tip, I will look into. Could you give an example how it could be implemented here? Have not used CTEs previously.
    – Meettu
    Sep 25 '20 at 20:35
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.