equivalent of nested if statement in mysql? - mysql

I need a table that calculates percentage changes between columns of another table (current value - previous value) / previous value
if a value goes from 0 to anything the result should be 1, otherwise it would cause a division by 0 error.
if the current value is also 0, return 0. otherwise calculate the percentage change
how do I replicate this if block in sql?
if previous value == 0
if current value == 0
return 0
return 1
return (current value - previous value) / previous value

If you want to nest the if statements in an select you can do it like this:
if(condition1, if(condition2, inner_true, inner_false), outer_false)
I don't necessarily claim that's the best solution to your problem compared to other ways to structure the query overall, but it does nest the if statements, as you requested.
If I understand what you meant by your non-select example, I think that would be for your case
select if(previous_value=0, if(current_value=0,0,1), (current_value-previous_value) / previous_value)

Related

Google Sheets: Find Row In a Column That Last Had A Larger Value Than The Current Row

Updated: While the solution provided =IF(A2>A1,IF(A2>MAX(A$1:A1),ROW()-1,IFERROR(B1+1,1)),1) does work for the original test data, it doesn't work for a more complex data set, see the second screen shot below:
Original question:
I have a need to process a column (A in the example) of numbers that represents a value changing over time, and establish for how many rows the present row's number has been the largest number, and report that as illustrated in Column B.
What I can't figure out is whether there is a way of producing column B using spreadsheet functions or if I need to write some apps script to do the calculations. I've looked at the usual suspects like MAX() and LARGE() but they don't quite do what I want.
What I want is something like MAXSINCE(A99, A:A98) but that doesn't exist.
Updated data set which still doesn't have an answer for the question: for how many rows has this row had the largest value?
Logic Flow:
Check if current value A2 is greater than previous value A1; If not, return 1
If the above is true, Check whether current value is greater than the present MAX. If so, return current ROW's number - starting offset 1 else add 1 to previous value B1
Code Sample:
B2:
=IF(A2>A1,IF(A2>MAX(A$1:A1),ROW()-1,IFERROR(B1+1,1)),1)
Drag fill down

zabbix - trigger on multiple items greater than 0

I have multiple items that are traps that return an integer such as below.
app.tidal.Health.HighPriority.MessagesInQueue
app.tidal.Health.CommDefault.MessagesInQueue
app.tidal.Health.Default.MessagesInQueue
I want to create a trigger if two or more of these has returned a value of greater than 0 in the last 3 checks to send a severity High message.
I'm having a hard time trying to devise my trigger this is what I currently have:
{Template_App_Tidal_Masters:app.tidal.Health.CommDefault.MessagesInQueue.min(#3)}>0 and
{Template_App_Tidal_Masters:app.tidal.Health.Default.MessagesInQueue.min(#3)}>0 and
{Template_App_Tidal_Masters:app.tidal.Health.HighPriority.MessagesInQueue.min(#3)}>0
But obviously it won't work as it's an and statement so all 3 would have to be greater than 0 the last 3 checks. Formatted the trigger on 3 lines to make it clearer.
This should work:
({Template_App_Tidal_Masters:app.tidal.Health.CommDefault.MessagesInQueue.min(#3)}>0) +
({Template_App_Tidal_Masters:app.tidal.Health.Default.MessagesInQueue.min(#3)}>0) +
({Template_App_Tidal_Masters:app.tidal.Health.HighPriority.MessagesInQueue.min(#3)}>0) > 1
Each part first evaluates an individual item to be larger than 0. If that is true, that part of the expression evaluates to 1, if false - to 0. In the end we sum up the results of these evaluations (not the original item values) and check whether two or more items had values larger than zero.

How to display only the negative values, and make all the positive values display as 0?

I have written a query to display this data, the last column gives data by using this query.
TIMESTAMPDIFF(MINUTE,booking_activity.activity_time,booking.pick_up_time) AS ActualWaitingTime
Now, I have to only display the negative values, and all the positive values should become 0.
How should this query be edited?
Try:
CASE
WHEN TIMESTAMPDIFF(MINUTE,booking_activity.activity_time,booking.pick_up_time) < 0
THEN TIMESTAMPDIFF(MINUTE,booking_activity.activity_time,booking.pick_up_time)
ELSE 0
END AS ActualWaitingTime
you can use the CASE function to make anything greater than or equal to zero as zero
or alternatively in your WHEN clause to only return values less than zero

SQL Server Zero Values

I am pretty green at this stuff and have a problem with what I thought was a simple select and calculate SQL Formula.
The issue being is that in my table I have a lot of zero values, sound's wierd I know but this is how it is.
The formula is to calculate a percentage marin based on two columns these being Total and Costs, whilst my simple formula works if there's a value, when theres a zero value absolutely nothing works.
The formula I am using is this ((Total-Costs)/Total)*100
Can anyone advise this greenhorn how to overcome the zero value issue?
You could do something like this
SELECT COALESCE((Total- Costs / NULLIF(Total,0))*100, 0)
If Total has a 0 in it, you'll get a divide by zero error. You must catch these using a CASE statement, like so
SELECT
CASE WHEN Total = 0 then 0 else ((Total-Costs)/Total)*100 END AS Percentage
FROM
Tbl

SSRS Conditional Summing

I have an SSRS report that displays several pages of rows. In each row is a "TYPE" field. In that TYPE field there is either an "M" for the value or a "P" for the value. At the end of the report I want to summ up all the price values for the "P" TYPES. I tried this but it prioduced an #Error:
=Sum(iif(Fields!TYPE.Value = "P",Fields!EXT_QTY.Value * Fields!PRICE.Value ,0))
this summed all rows
=iif(Fields!PART_TYPE.Value = "P" , Sum(Fields!EXT_QTY.Value * Fields!PRICE.Value ), 0 )
I'm sure this is do-able. Any ideas? Thanks
Found the answer....
=SUM(IIF(Fields!PART_TYPE.Value ="P",CDbl(Fields!EXT_QTY.Value * Fields!PRICE.Value), CDbl(0.0)))
The SUM fails due to type comparison - you can't Sum values of different types, being the expression (probably a Double) with 0, an Integer. MikeTWebb's answer does explicit type conversion to get around this error. This is fine for this specific example, being a Sum, however this doesn't produce an accurate result if you want an average (being Sum / Count) of the values where the Type is P. That is because 0 is a value and would be included in the averaging calculation when you actually want those values excluded from the calculation.
Another option is to use Nothing instead of 0:
=Sum(IIF(Fields!TYPE.Value = "P", Fields!EXT_QTY.Value * Fields!PRICE.Value, Nothing))
This solves the type comparison error without needing explicit typecasting and is a better solution when you are using aggregations where whether the value exists or not is significant to the result, like Average.