Solved

SQL Calulation Problem

Posted on 2008-10-28
7
302 Views
Last Modified: 2010-04-21
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
Comment
Question by:nutnut
  • 3
  • 2
  • 2
7 Comments
 
LVL 37

Expert Comment

by:momi_sabag
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

by:nutnut
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

by:TechSinger
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 37

Accepted Solution

by:
momi_sabag earned 400 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

by:TechSinger
TechSinger earned 100 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

Open in new window

0
 
LVL 3

Expert Comment

by:TechSinger
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

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

Thanks

nutnut
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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. …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

861 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

26 Experts available now in Live!

Get 1:1 Help Now