Solved

SQL Calulation Problem

Posted on 2008-10-28
7
306 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Technology Partners: 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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

680 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