2

I have two tables with same field types. Fields that are common in here are *BG_ID* and Store Number. Now I want to SUM two columns (*total_area* and *total_area_blk*) from two tables and group them by BG_ID .

The following is what I tried which doesnt work:

 select t.bg_id, t.store_number,sum(a.total_area),sum(b.total_area_blk)
 from (select bg_id,store_number,total_area
 from temp_Prop_area_block a
 where store_number='33665'
 union all
 select bg_id,store_number,total_area_blk 
 from temp_Prop_area_BG b
 where store_number='33665')

1 Answer 1

3

Hope, this helps you!

SELECT bg_id,  store_number,  SUM(total_area)
FROM
  (
    SELECT bg_id,  store_number,  total_area as total_area
    FROM temp_Prop_area_block a
    WHERE store_number='33665'
    UNION ALL
    SELECT bg_id,   store_number,  total_area_blk as total_area
    FROM temp_Prop_area_BG b
    WHERE store_number='33665'
  ) my_view
GROUP BY bg_id, store_number;
Sign up to request clarification or add additional context in comments.

6 Comments

area fields are different in two tables . one is total_area and other is total_area_blk
When we do union, the resultset will be clubbed and the column alias will turn up as the first UNION part's column name.. So now, total_area itself will do, and it holds both values.. Added alias to my query for readability!
This one returns no output.
Just try to run the Inner Sub Query only and see the results.. Reason should be with your WHERE condition.
This also doesnt return any output.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.