[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Query with SUM

Posted on 2011-10-05
Medium Priority
Last Modified: 2012-05-12
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.

Question by:wakatanka
  • 4
  • 2
LVL 74

Expert Comment

ID: 36918401
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
LVL 74

Expert Comment

ID: 36918434

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 Comment

ID: 36918444
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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 74

Expert Comment

ID: 36918462
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
LVL 74

Accepted Solution

sdstuber earned 2000 total points
ID: 36918465
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

Author Closing Comment

ID: 36918481
Speedy answer and very clear. great.

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question