Solved

SQL Calulation Problem

Posted on 2008-10-28
7
301 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 37

Accepted Solution

by:
momi_sabag earned 400 total points
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks both of you.  Both solved problem but am going with momi_sabag's

Thanks

nutnut
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Restore Procedure question 4 28
MS SQL Backup 24 67
Ranking Based On Value 3 28
SQL Inner Join Vs SubQueries 9 23
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

762 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

7 Experts available now in Live!

Get 1:1 Help Now