Query with SUM

Hello, here is my SQL problem:

table A:

id_row | products | price |
-------------+----------+-------------
           1 |        PDA |      500.00 |    
           2 |        PDX |      650.00 |    
           3 |       PDB  |           3.00 |      
           4 |        PDC |      100.00 |            
           5 |       PDC  |           5.00 |      
           6 |       PDD  |       100.00 |    

table B:

id_row_table A | price |
---------------------+-------------
           1 |           500.00 |    
           1 |           650.00 |    
           2 |             40.00 |      
       
I would like to have this result view:


id_row | product | price | pricesumfromtableB
---------------------+------------+--------------------------
           1 |   PDA |  500.00 |    1150.00
           2 |   PDX |  650.00 |         40.00
       

thanks to all.

wakatankaAsked:
Who is Participating?
 
sdstuberCommented:
alternately...  change the inner above to outer


sselect a.id_row,a.product,a.price,sum(b.price)
from a left outer join b
on  a.id_row = b.id_row_table_a
group by a.id_row,a.product,a.price
0
 
sdstuberCommented:
sselect a.id_row,a.product,a.price,sum(b.price)
from a,b
where a.id_row = b.id_row_table_a
group by a.id_row,a.product,a.price
0
 
sdstuberCommented:
also...

sselect a.id_row,a.product,a.price,sum(b.price)
from a inner join b
on  a.id_row = b.id_row_table_a
group by a.id_row,a.product,a.price
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
wakatankaAuthor Commented:
HI sdstuber, thanks for your solution, but in this way it returns only row that have some record in table b, i
need also all the other rows in table A.
0
 
sdstuberCommented:
your example doesn't show that,  it only shows rows that are in both tables...

but, it's easy enough, try this...


select a.id_row,a.product,a.price,(select sum(b.price) from b where  a.id_row = b.id_row_table_a)
from a
0
 
wakatankaAuthor Commented:
Speedy answer and very clear. great.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.