Solved

SUM Statement

Posted on 2008-10-31
Medium Priority
237 Views
Below is my select statement. I need to sum the three EST* columns, the the same for the thee ACT* columns, then create the difference. I must be missing something, I keep getting errors.

SELECT     BASE_ID, STATUS, PRODUCT_CODE, SCHED_START_DATE, EST_MATERIAL_COST, EST_LABOR_COST, EST_SERVICE_COST, ACT_MATERIAL_COST,
ACT_LABOR_COST, ACT_SERVICE_COST
FROM         WORK_ORDER
0
Question by:jethrow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions

LVL 26

Accepted Solution

tigin44 earned 500 total points
ID: 22853897
If you want to produce these calculations for each row then you can try the code below.

SELECT     BASE_ID, STATUS, PRODUCT_CODE, SCHED_START_DATE, (EST_MATERIAL_COST + EST_LABOR_COST + EST_SERVICE_COST) AS ESTIMATED_COST, (ACT_MATERIAL_COST +
ACT_LABOR_COST + ACT_SERVICE_COST) AS ACTUAL_COST, (EST_MATERIAL_COST + EST_LABOR_COST + EST_SERVICE_COST) - (ACT_MATERIAL_COST +
ACT_LABOR_COST + ACT_SERVICE_COST) AS DIFFERENCE
FROM         WORK_ORDER

but if you want to produce total cost for two or more rows then you must use aggrate functions

SELECT     BASE_ID, STATUS, PRODUCT_CODE, SCHED_START_DATE, (sum(EST_MATERIAL_COST) + sum(EST_LABOR_COST) + sum(EST_SERVICE_COST)) AS ESTIMATED_COST, (sum(ACT_MATERIAL_COST) + sum(ACT_LABOR_COST) + sum(ACT_SERVICE_COST)) AS ACTUAL_COST, (sum(EST_MATERIAL_COST) + sum(EST_LABOR_COST) + sum(EST_SERVICE_COST)) - (sum((ACT_MATERIAL_COST) +
sum(ACT_LABOR_COST) + sum(ACT_SERVICE_COST)) AS DIFFERENCE
FROM         WORK_ORDER
WHERE some condition
GROUP BY  BASE_ID, STATUS, PRODUCT_CODE, SCHED_START_DATE
0

Author Closing Comment

ID: 31512199
Thanks for the quick responce! Perfect!
0

Featured Post

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

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

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