Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
display 1 table and retreive sum from another
Old 09-28-2006, 02:06 AM display 1 table and retreive sum from another
Experienced Talker

Posts: 35
Trades: 0
I would like to display the 1st table. The CreditTotal should be displaying the sum from table 2 where Table2.InventoryID matches Table1.ID

The first row CreditTotal should be showing 2000. The second row CreditTotal should be displaying 600. I have no clue how to do it. Do you? Thanks.





Table 1: Inventory
ID-----Company Name-----------CreditTotal
1 -------- Company A ------------------
2 -------- Company B ------------------



Table 2: Credit
ID ---InventoryID ---------- Credit
1 ----------- 1 ------------------ 1200
2 ----------- 1 -------------------600
3 ----------- 1 -------------------200
4 ----------- 2 -------------------500
5 ----------- 2 -------------------100
webdesigner555 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-28-2006, 03:13 AM Re: display 1 table and retreive sum from another
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
use a join and a GROUP BY

SELECT companyname,sum(cred.credit) as ct from credit as cred left join inventory on inventory.id = cred.inventoryid group by companyname ;

NOTE:
Avoid using a field with the same name as the table, it will cause ambiguity errors when using some types of queries.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-28-2006, 04:04 AM Re: display 1 table and retreive sum from another
Experienced Talker

Posts: 35
Trades: 0
wow, thanks .
I was monkeying around with this for many hours now. I modified your code to right join so that it displays null values also.

If I may.... ask one more thing: There is another column in Table1 called TotalValue. Again the total needs to be summed up from table2 where Table1.ID = Table2.InventoryID

Totalvalue on row 1 should be 60 and 40 on row 2. I appriciate your help.


Table 1: Inventory
ID-----Company Name-----------CreditTotal------------TotalValue
1 -------- Company A ------------------ 2000----------------------
2 -------- Company B ------------------ 600------------------------



Table 2: Credit
ID ---InventoryID ---------- Credit-------Value
1 ----------- 1 ------------------ 1200------------20
2 ----------- 1 -------------------600-------------20
3 ----------- 1 -------------------200-------------20
4 ----------- 2 -------------------500-------------20
5 ----------- 2 -------------------100-------------20
webdesigner555 is offline
Reply With Quote
View Public Profile
 
Old 09-28-2006, 05:34 AM Re: display 1 table and retreive sum from another
Experienced Talker

Posts: 35
Trades: 0
nevermind, I got it.

Thanks again.
webdesigner555 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to display 1 table and retreive sum from another
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13797 seconds with 12 queries