Percentage of two values returning NULL - sql

Same as yesterdays question which has been answered successfully but different problem. I have two values, 1 and 0 for which I need to calculate the percent change. Based on this website http://www.percent-change.com/index.php?y1=1&y2=0 the percent change between 1 and 0 is -100%. Based on the suggested formula which is (((y2- y1))/ y1) my code looks like this.
DefinedYearVSPriorYearIndividual = ((( CTEDefinedYear.IndividualCases - CTEPreviousYear.IndividualCasesLastYear ))
/ ( CTEPreviousYear.IndividualCasesLastYear ) ) * 100
which returns NULL.
The two numbers are
CTEDefinedYear.IndividualCases = 1
CTEPreviousYear.IndividualCasesLastYear = 0
The desired result should be -100%.
Can anybody see what I'm doing wrong?

Here is the answer.
Declare #y1 as int =1;
Declare #y2 as int =0;
select (((#y2- #y1))/ #y1)*100
Output is -100. You missed the *100 part.

In your case, You switched variables. attached formula is right one.
select ((0 - 1) / 1)*100;
But you used select ((1 - 0) / 0)*100;
so, you will get an error:
Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.

You have to handle 0 in the division side, with CASE logic, to avoid divide by zero error.
DECLARE #CTEDefinedYear_IndividualCases INT = 1
DECLARE #CTEPreviousYear_IndividualCasesLastYear INT = 0
SELECT ((#CTEDefinedYear_IndividualCases - #CTEPreviousYear_IndividualCasesLastYear) / (CASE WHEN #CTEPreviousYear_IndividualCasesLastYear = 0 THEN 1 ELSE #CTEPreviousYear_IndividualCasesLastYear END)) * 100

Got it to work with this code.
DefinedYearVSPriorYearIndividual = ISNULL(100.0 *
(ISNULL(CTEDefinedYear.IndividualCases,0)
- ISNULL(CTEPreviousYear.IndividualCasesLastYear,0))
/ NULLIF(CTEPreviousYear.IndividualCasesLastYear,0),0)

Related

calculated field using if and or in ms-sql

IF(
[dbo.tblx.Category] = 'WS' OR [dbo.tblx.Category] = 'SEM',
0,
[dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
)
this returns the entire text and not the "calculation"
can someone help me...what am I doing wrong?
i tried
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000
END AS Metal
Do perhaps insert something before "CASE WHEN"? it is not running
As I understood, you are trying to create a computed column, using value from other table.
I think in this case you have to use a User Defined function in the formula for computed column. If you give more information I can try to write some code for you.
Instead, if you just want to add a column in a query you can use:
CASE WHEN dbo.tblx.Category = 'WS' OR dbo.tblPTx.Category = 'SEM' THEN 0
ELSE dbo.tblx.Tonnes * dbo.tblx.Grade / 1000 END AS NAME_OF_YOUR_NEW_COLUMN
Update
DECLARE #myvar INT;
SET #myvar = CASE WHEN ([dbo.tblx.Category] = WS OR [dbo.tblPTx.Category] = SEM) THEN 0
ELSE [dbo.tblx.Tonnes] * [dbo.tblx.Grade] / 1000
END

Mathematical Function within Sql Case Statement

I am trying to come up with a sql statement which converts the odometer if stored in km to miles. If the odometer is stored in miles, it leaves as it is.
After the conversion, it then needs to check for Search paramters i.e Mileage.
The steps I have taken is using the Case Statement.
Here is my snippet of the select statement that I am using currently:
DECLARE
#Mileage NVARCHAR(75) = NULL,
#IsMiles BIT = 1,
#Converted NVARCHAR(75) = NULL
SELECT [Id],Odometer,IsMiles,
CASE IsMiles when 0 THEN OdometerValue * 0.62137
else Odometer end
FROM [dbo].[Vehicle]
where IsMiles = 0
Is there anyway to pass the Result of the case statement to ConvertedOdometer. I want to use that value to evaluate against the search Mileage parameters.
Something like this with this condition:
(ConvertedOdometer >=0 AND ConvertedOdometer <= #Mileage)
I am new to Case statement so have used these guides:
StackOverflow
Sql School
Some Blog
Perhaps something like this ...
DECLARE
#Mileage NVARCHAR(75) = NULL,
#IsMiles BIT = 1,
#Converted NVARCHAR(75) = NULL
select a.* from
(SELECT [Id],Odometer,IsMiles,
CASE when IsMiles=0 THEN OdometerValue * 0.62137 else Odometer end as ConvertedOdometer
FROM [dbo].[Vehicle]
where IsMiles = 0)a
where a.ConvertedOdometer >=0 AND
a.ConvertedOdometer <= #Mileage

Divide by zero error in SQL even though I excluded the zero cases?

Here is the code that I've been trying to run:
SELECT C.* FROM
(SELECT
B.[OUTSIDE_ROW],
B.[INSIDE_ROW],
B.[r_HU_vac_ns],
B.[r_HU_vac_ns_MOE],
CASE WHEN B.[r_HU_vac_ns] = 0 THEN 999 ELSE B.[r_HU_vac_ns_MOE]/B.[r_HU_vac_ns] END AS [PCT]
FROM
(SELECT
A.[OUTSIDE_ROW],
A.[INSIDE_ROW],
(A.[HU_VACANT] - A.[HU_VACANT_SEASONAL_RECREATIONAL])/A.[HU_VACANT] AS [r_HU_vac_ns],
(1/A.[HU_VACANT]) * POWER(
CASE WHEN ((A.[HU_VACANT] - A.[HU_VACANT_SEASONAL_RECREATIONAL])/A.[HU_VACANT]) * POWER(A.[HU_VACANT_MOE], 2) < POWER(A.[HU_VACANT_MOE], 2) + POWER(A.[HU_VACANT_SEASONAL_RECREATIONAL_MOE], 2) THEN POWER(A.[HU_VACANT_MOE], 2) + POWER(A.[HU_VACANT_SEASONAL_RECREATIONAL_MOE], 2) - (((A.[HU_VACANT] - A. [HU_vACANT_SEASONAL_RECREATIONAL])/A.[HU_VACANT]) * POWER(A.[HU_VACANT_MOE], 2))
ELSE POWER(A.[HU_VACANT_MOE], 2) + POWER(A. [HU_VACANT_SEASONAL_RECREATIONAL_MOE], 2) + (((A.[HU_VACANT] - A. [HU_vACANT_SEASONAL_RECREATIONAL])/A.[HU_VACANT]) * POWER(A.[HU_VACANT_MOE], 2)) END, 0.5) AS [r_HU_vac_ns_MOE]
FROM
(SELECT
[OUTSIDE_ROW],
[INSIDE_ROW],
SUM([ESTIMATE_1]) AS [HU_VACANT],
POWER(SUM(POWER([MOE_1], 2)), 0.5) AS [HU_VACANT_MOE],
SUM([ESTIMATE_2]) AS [HU_VACANT_SEASONAL_RECREATIONAL],
POWER(SUM(POWER([MOE_2], 2)), 0.5) AS [HU_VACANT_SEASONAL_RECREATIONAL_MOE]
FROM #TEST_TABLE
GROUP BY [OUTSIDE_ROW], [INSIDE_ROW]) A
WHERE A.[HU_VACANT] > 0) B ) C
WHERE C.[PCT] < 0.2
Every time I run it, I get the following error:
Msg 8134, Level 16, State 1, Line 533
Divide by zero error encountered.
However, if I take off the last line of code (the following WHERE clause) the code runs fine:
WHERE C.[PCT] < 0.2
Just from looking at my query, can anyone tell me what I'm doing wrong? I thought I eliminated all PCT values that were zero with the CASE WHEN statement below so this error is baffling me:
CASE WHEN B.[r_HU_vac_ns] = 0 THEN 999 ELSE B.[r_HU_vac_ns_MOE]/B.[r_HU_vac_ns] END AS [PCT]
If it helps, PCT is cast as floating point.
Thanks.
SQL Server reserves the right to rearrange calculations. That means that the calculation in a SELECT can happen before filtering occurs. This is true even when the filters are in subqueries and CTEs.
The only way to guarantee order of calculation is CASE. However, I think it is easier to just use NULLIF(), an ANSI standard function. Instead of logic like this:
(A.[HU_VACANT] - A.[HU_VACANT_SEASONAL_RECREATIONAL])/A.[HU_VACANT] AS [r_HU_vac_ns],
do:
(A.[HU_VACANT] - A.[HU_VACANT_SEASONAL_RECREATIONAL])/NULLIF(A.[HU_VACANT], 0) AS [r_HU_vac_ns],
Instead of filtering out the records using
WHERE A.[HU_VACANT] > 0
You should filter out the records at root level
having SUM([ESTIMATE_1]) > 0
You could also use nullif function but that will result producing NULL where you have zero

Invalid floating point operation with CTE

I'm getting An invalid floating point operation occurred error with this code.
It's telling me that the problem is on line where the CTE starts.
If I don't have the SQRT function, then everything work fine, but as soon as I add the SQRT I get the error.
What I'm trying to do is to get rid of the negative by squaring the number.
declare #start_date date, #end_date date
set #start_date = '2012/01/01'
set #end_date = '2012/12/31';
with tbl1
as
(
select
strata,
entranceID,
sum(count)/count(*) as AverageCount
from
train
where
surveydate between '2012/01/01' and '2012/12/31'
group by
strata,
entranceID
)
select
a.jobnumber,
a.strata,
a.EntranceID,
Count,
b.AverageCount,
count - AverageCount
, CASE WHEN AverageCount = 0 then 0
ELSE SQRT(count - AverageCount)
END as A
In the expression:
SQRT(count - AverageCount)
The value of count has to be >= AverageCount.
This may not be true for your test data.
Consider using :
SQRT (ABS(count - AverageCount))
Instead, If that makes sense in your case.

Latitude and Longitude conversion error

I'm trying to execute a specific query in my database, but occurs the following error
Table San_Filial
Filial_Id Name lat lon
2 A -19.926131 -43.924373
3 B -19.952192 -43.938789
4 C -19.939626 -43.924541
5 D -19.95529 -43.92953
6 E -19.9099 -43.93124
7 F -19.926191 -43.946067
9 G -19.97125 -43.96622
14 H -19.89038 -43.921734
17 I -19.88838 -43.93059
19 J -19.94305 -43.94093
Query
SELECT *
FROM San_Filial
WHERE San_Filial.Credenciada_Id IN (2,3,4,5,6,7,9,14,17)
AND ACOS(COS(RADIANS(ltrim(San_Filial.lat)))
* COS(RADIANS(convert(float, -19.926131)))
* COS(RADIANS(ltrim(San_Filial.lon))
- RADIANS(convert(float, -43.924373)))
+ SIN(RADIANS(ltrim(San_Filial.lat)))
* SIN(RADIANS(convert(float, -19.926131)))) * 6380 < 5.0
Error
Mesage 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
Someone can help me ?
Start by seeing if you have any data that is not in a correct format:
select *
from san_filia1
wHERE San_Filial.Credenciada_Id IN (2,3,4,5,6,7,9,14,17) and
(isnumeric(lat) = 0 or isnumeric(long) = 0)
From this, you'll see what is causing the problem and then you can fix it.
The correct fix is probably along these lines:
select *
from (select sf.*,
(case when isnumeric(lat) then cast(lat as float) end) as latf,
(case when isnumeric(long) then cast(long as float) end) as longf
from san_filial sf
) sf
where . . . -- use Latf and Longf instead of lat and long
You need to do the conversion inside a case statement to guarantee that it works as expected. SQL does not guarantee the ordering of statements and a filter might be applied after the calculation.