3

I have two tables where one of the tables, TABLE2, has a column for TABLE1_IDs, thus there is a many-to-one relationship between TABLE2 and TABLE1 rows. TABLE2 has a column PRICE which is a number that represents a dollar amount. I have a query that obtains certain rows of TABLE1, but I want to get the total of all corresponding TABLE2 rows' PRICE values as an additional column in the query results.

How do I accomplish this in Oracle?

2
  • 2
    have you tried SQL? it is pretty cool :) Commented Aug 12, 2011 at 20:56
  • Yeah this should be pretty easy for someone who knows SQL. Commented Aug 12, 2011 at 20:57

2 Answers 2

4

Easy - join, and sum.

select t1.table1_id
,      sum(t2.price) total_price
from   table1 t1
,      table2 t2
where  t1.table1_id = t2.table1_id
group  by t1.table1_id;
Sign up to request clarification or add additional context in comments.

Comments

3

I believe you want something like this:

SELECT A.Id, SUM(B.Price) TotalPrice
FROM (  SELECT *
        FROM Table1
        WHERE Something) A
LEFT JOIN Table2 B
ON A.Id = B.Table1_id
GROUP BY A.Id

Comments

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.