Solved

SQL Calulation Problem

Posted on 2008-10-28
7
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

732 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