| | |
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:
Solved Threads: 0
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
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
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
tables are as follows
MS SQL Syntax (Toggle Plain Text)
neworder part_id(PRIMARY KEY) number_of_parts parts part_id(PRIMARY KEY) 7-8 more COLUMNS, eg size, DESC, sup id 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)
TABLE new ORDER part_id number_of_parts pt12 2 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
1
#2 32 Days Ago
MS SQL Syntax (Toggle Plain Text)
IF OBJECT_ID('tempdb..#NewOrders', 'U') IS NOT NULL DROP TABLE #NewOrders IF OBJECT_ID('tempdb..#Parts', 'U') IS NOT NULL DROP TABLE #Parts CREATE TABLE #NewOrders ( PartId varchar(10) PRIMARY KEY, number_of_parts int ) CREATE TABLE #Parts ( PartId varchar(10) PRIMARY KEY, [Description] varchar(100), Cost money ) INSERT INTO #Parts (PartId, Cost, [Description]) Values ('pt12', 5.00, 'A very cool part') INSERT INTO #Parts (PartId, Cost, [Description]) Values ('2pt255', 7.22, 'A not so neat part') INSERT INTO #NewOrders (PartId, number_of_parts) Values ('pt12', 500) INSERT INTO #NewOrders (PartId, number_of_parts) Values ('2pt255', 900) SELECT #NewOrders.PartId, #NewOrders.number_of_parts, #Parts.Cost, (#NewOrders.number_of_parts * #Parts.Cost) As TotalCost FROM #NewOrders Inner Join #Parts On (#NewOrders.PartId = #Parts.PartId)
Results in:
text Syntax (Toggle Plain Text)
PartId number_of_parts Cost TotalCost ---------- --------------- --------------------- --------------------- 2pt255 900 7.22 6498.00 pt12 500 5.00 2500.00
![]() |
Similar Threads
- Sum all the values in one colum of a html table? (PHP)
- Collect sum of a column in a html table? (JavaScript / DHTML / AJAX)
- Copy records from one table to samme table in another sql server (MS SQL)
- Select SUM (All Fields in table) (MySQL)
- Trying to find character occurrences in table field using SUM??? (MySQL)
- using a variable to name the table in the CREATE TABLE function (PHP)
- Create an extra table (copy the columns and new table) for every existing table (MS SQL)
- CSS -- table border and table header color (HTML and CSS)
Other Threads in the MS SQL Forum
- Previous Thread: UPDATE with INNER JOIN
- Next Thread: Loop to generate future dates?!
| Thread Tools | Search this Thread |






