944,116 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 699
  • MS SQL RSS
Oct 27th, 2009
0

Getting sum of cost from one table where quantity on another table

Expand Post »
hi guys, I have 2 tables, one called neworders which stores the part_id and num or parts and a 2nd table called parts which has all the part details including cost.
tables are as follows
MS SQL Syntax (Toggle Plain Text)
  1. neworder
  2. part_id(PRIMARY KEY)
  3. number_of_parts
  4.  
  5. parts
  6. part_id(PRIMARY KEY)
  7. 7-8 more COLUMNS, eg size, DESC, sup id
  8. cost

what i need to do is get the sum of the costs based on the quantity of the parts ordered

so if table is as follows

MS SQL Syntax (Toggle Plain Text)
  1. TABLE new ORDER
  2. part_id number_of_parts
  3. pt12 2
  4. pt255 4

I need to use the part_id of the neworder table to get the costs of the parts and then add them up with sum based on number of parts ordered.

I have no idea how to do this, any help would be awesome
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
mr_scooby is offline Offline
58 posts
since Feb 2008
Oct 27th, 2009
1
Re: Getting sum of cost from one table where quantity on another table
MS SQL Syntax (Toggle Plain Text)
  1. IF OBJECT_ID('tempdb..#NewOrders', 'U') IS NOT NULL DROP TABLE #NewOrders
  2. IF OBJECT_ID('tempdb..#Parts', 'U') IS NOT NULL DROP TABLE #Parts
  3. CREATE TABLE #NewOrders
  4. (
  5. PartId varchar(10) PRIMARY KEY,
  6. number_of_parts int
  7. )
  8.  
  9. CREATE TABLE #Parts
  10. (
  11. PartId varchar(10) PRIMARY KEY,
  12. [Description] varchar(100),
  13. Cost money
  14. )
  15.  
  16. INSERT INTO #Parts (PartId, Cost, [Description]) Values ('pt12', 5.00, 'A very cool part')
  17. INSERT INTO #Parts (PartId, Cost, [Description]) Values ('2pt255', 7.22, 'A not so neat part')
  18.  
  19. INSERT INTO #NewOrders (PartId, number_of_parts) Values ('pt12', 500)
  20. INSERT INTO #NewOrders (PartId, number_of_parts) Values ('2pt255', 900)
  21.  
  22. SELECT #NewOrders.PartId, #NewOrders.number_of_parts, #Parts.Cost, (#NewOrders.number_of_parts * #Parts.Cost) As TotalCost
  23. FROM #NewOrders Inner Join #Parts On (#NewOrders.PartId = #Parts.PartId)

Results in:
text Syntax (Toggle Plain Text)
  1. PartId number_of_parts Cost TotalCost
  2. ---------- --------------- --------------------- ---------------------
  3. 2pt255 900 7.22 6498.00
  4. pt12 500 5.00 2500.00
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 27th, 2009
0
Re: Getting sum of cost from one table where quantity on another table
that is awesome thanks dude.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
mr_scooby is offline Offline
58 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: UPDATE with INNER JOIN
Next Thread in MS SQL Forum Timeline: Loop to generate future dates?!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC