Solved

# SQL Calulation Problem

Posted on 2008-10-28
Medium Priority
323 Views
Hi

To simplify this question I have a view containing 9 columns

ID, CalcId,SequenceId,VariableType,From,To,ActualMins,ActualWeight, Rate

it contains 2 rows of data

1,1,1,MINUTES, 0, 180, 150, 63, 10
2,1,2,WEIGHT,0,65,150,63, 10

I am trying to add/calculate a 10th column thats called "Result"

I need help please writing the SQL to calculate the following from the above data please

"If the Actualminutes (150) falls between From (0) and To (180) THEN return ActualWeight (63) * Rate (10) = 630 in the Result column.  But I only require this result on the WEIGHT row and not the MINUTES row, so final table would look like below

1,1,1,MINUTES, 0, 180, 150, 63, 10, 0
2,1,2,WEIGHT,0,65,150,63, 10, 630

(if 150 was say 190 and is NOT between 0 and 180 which its not then would return below, note the zero instead of 630

1,1,1,MINUTES, 0, 180, 150, 63, 10, 0
2,1,2,WEIGHT,0,65,150,63, 10, 0

Thanks

nutnut

0
Question by:nutnut
• 3
• 2
• 2

LVL 37

Expert Comment

ID: 22823235
select *, case when Actualminutes between 0 and 180 and VariableType='WEIGHT' then ActualWeight * Rate else 0 end
from your_table
0

Author Comment

ID: 22823333
thanks momi_sabag

I may have mislead you with the question I asked but this will not work as 0, 180 and WEIGHT are never on the same row

1,1,1,MINUTES, 0, 180, 150, 63, 10, 0
2,1,2,WEIGHT,0,65,150,63, 10, 0

Any ideas would be welcomed please

Thanks

nutnut
0

LVL 3

Expert Comment

ID: 22823375
It appears that you have three separate scenarios.
1. VariableType is MINUTES
2. VariableType is WEIGHT and ActualMins falls between 0 and 180
3. VariableType is WEIGHT and ActualMins is greater than 180.
What you can do is make them separate queries with the 10th column being the appropriate value in that query.  You can then combine them using the UNION command.  If you need them sorted, you can put an ORDER BY at the end.
0

LVL 37

Accepted Solution

momi_sabag earned 1600 total points
ID: 22823379

select t2.*, case when t2.Actualminutes between t1.from and t1.to and t2.VariableType='WEIGHT' then t2.ActualWeight * t2.Rate else 0 end
from your_table t1 inner join your_table t2 on t1.CalcId  =t2.CalcId
where t2.VariableType='WEIGHT'
and t1.VariableType='MINUTES'
0

LVL 3

Assisted Solution

TechSinger earned 400 total points
ID: 22823703
If you would like an example of some SQL code I would use as I explained in my comment, I've go it below.  I combined the scenarios 1 and 3 into on query since they would produce the same results:

``````SELECT T1.*,0 AS Result
FROM tblMyTable T1
WHERE T1.VariableType='MINUTES'
OR T1.ActualMins > 180
OR T1.ActualMins < 0
UNION
SELECT T2.*,T2.ActualWeight*T2.Rate AS Result
FROM tblMyTable T2
WHERE T1.VariableType='WEIGHT'
AND T2.ActualMins <= 180
AND T1.ActualMins >= 0
ORDER BY ID
``````
0

LVL 3

Expert Comment

ID: 22824803
There are some typos in the code a presented.  Line 9 and 10 should use T2 instead of T1.
0

Author Closing Comment

ID: 31510797
Thanks both of you.  Both solved problem but am going with momi_sabag's

Thanks

nutnut
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.