# 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.

###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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
Commented:
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
Author 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.
Commented:
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
Commented:
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Speedy answer and very clear. great.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PostgreSQL

From novice to tech pro — start learning today.