Solved

SUM Statement

Posted on 2008-10-31
2
235 Views
Last Modified: 2010-04-21
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.

Help Please.


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
Comment
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
2 Comments
 
LVL 26

Accepted Solution

by:
tigin44 earned 125 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

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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

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

Join & Ask a Question