Solved

SUM Statement

Posted on 2008-10-31
2
231 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
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now