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

Please support our MS SQL advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 35
Reputation: mr_scooby is an unknown quantity at this point 
Solved Threads: 0
mr_scooby mr_scooby is offline Offline
Light Poster

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

 
0
  #1
32 Days Ago
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
  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

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,210
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 572
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
1
  #2
32 Days Ago
  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:
  1. PartId number_of_parts Cost TotalCost
  2. ---------- --------------- --------------------- ---------------------
  3. 2pt255 900 7.22 6498.00
  4. pt12 500 5.00 2500.00
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 35
Reputation: mr_scooby is an unknown quantity at this point 
Solved Threads: 0
mr_scooby mr_scooby is offline Offline
Light Poster
 
0
  #3
32 Days Ago
that is awesome thanks dude.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC