Replace NULL result as value - replace

can anyone help me with these:
select Amount1, Amount2, Value1, Value2, 100(SUM(NULLIF(Amount1, 100)/NULLIF(Amount2, 0))-1) as [Amount %], 100(SUM(NULLIF(Value1, 0)/NULLIF(Value2, 0))-1) as [Value %]**
My problem is: when I get the result there is some columns with 0 (Amount1, Amount2, Value1, Value2). When I tried to get a percentage (Amount % and Value %), with this this formula you can see, there is a lot NULL values because if there is only one of that columns has 0 and it can't be counted with 0. Does anyone have a formula or edit this, to show me a value 100 or -100 in Amount % and Value % as a result when any of these columns (Amount1, Amount2, Value1, Value2) have 0?
Thanks.

Related

I try to divide two functions from each other and get 0 every time

I have the code underneat in Amazone Athena to calculate how the last 7 days in footfall compare to the average week last year.
I only get a 0 as a result all the time, what is the problem.
I tried to make the x and y as Float but that still gave zero's
The data is on daily basis and a calculate a week average of last year by addding all weeks and divide by 52 (probably also a better way to do this)
select x.visitors/y.visitors*100
from
(select sum(visitors) as visitors
from corrected_scanners_per_day
where btcode in ('BT120031', 'BT120000','BT902', 'BT120052', 'BT120050', 'BT130109', 'BT120131', 'BT130110', 'BT130107', 'BT120126', 'BT120078', 'BT120076', 'BT120035', 'BT130450', 'BT120063', 'BT120044', 'BT120082', 'BT120030', 'BT120116', 'BT121196', 'BT130366', 'BT120085', 'BT120053', 'BT120014')
And datetime
BETWEEN current_date - interval '7'day
AND current_date) x
Join
(select sum(visitors) / 52 as visitors, year(datetime) as year
from corrected_scanners_per_day
where btcode in ('BT120031', 'BT120000','BT902', 'BT120052', 'BT120050', 'BT130109', 'BT120131', 'BT130110', 'BT130107', 'BT120126', 'BT120078', 'BT120076', 'BT120035', 'BT130450', 'BT120063', 'BT120044', 'BT120082', 'BT120030', 'BT120116', 'BT121196', 'BT130366', 'BT120085', 'BT120053', 'BT120014')
And datetime
BETWEEN CAST('2019-01-01' AS timestamp)
AND CAST('2019-12-31' AS timestamp)
group by year(datetime)
order by year(datetime)) y on 1=1
This is happening because your data types (visitors and 100) are all INT, so your output data type will also be an INT. When the calculation is converted to an INT, it is probably rounding to 0. So you need to explicitly make sure your final data type is a numeric type that allows decimals.
Try changing the first line to this:
select (x.visitors * 1.0) / (y.visitors * 1.0) * 100.0
You have other INT types inside your subqueries that should also probably be converted to decimals to make sure no truncation or rounding is occurring.

How to forecast the column value based on its previous value in Python

I have a dataframe with 3,000,000 IDs. Each ID has the month range from 2015-01-01
t0 2018-12-01. Each ID has column "A" and "B" with numeric values. I need to create a new column "C:.
For each ID, when Date == '2015-01-01' which is the first month for that ID, column C value equal to exp(column_A value).
For the next month (Date == '2015-02-01'), column C value equal to exp(log(column_C_value in previous month) + column_B_value at this month), so here is exp(log(column C # 2015-01-01) + column_B # 2015-02-01). Each of the following months has the same pattern until it reaches 2018-12-01.
In Python, I can setup the loop for each ID and for each row/month, such as:
for ID in range(xxx):
for month in range(xxxx):
However, such calculation takes long time. Can anyone tell me a faster way to do this calculation? Very appreciated for your help!
Consider that
(1) exp(x+y) == exp(x)*exp(y).
And
(2) exp(log(x)) == x.
So exp(log(c1) + b2) == c1 * exp(b2).
The next value simplifies to c1 * exp(b2) * exp(b3), and so on.
That means, you have to multiply all exp(b) values, which can be turned
into adding all(b)-s and then applying exp() to the result.
And don't forget to multiply it with a1, the initial value.
a1 * exp(b2 * b3 * ...)

How we can do this in google sheet? multiply based on condition

I have data in google sheet and I want to get this
I have numbers value in B Column and want to multiply based on condition, Like if numbers above 650 in column B then formula insert value in Column C 125 if value bellow 650 in column B then formula multiply value with 0.16 like 648*0.16 and want final result in column C. If value above 850 in Column B then also formula multiply value with 0.16 like 855*0.16 and final result in Column C .
Here is ex:
Sorry for the bad English.
I tried this
=B2 * IF(B2 > 650,(0+125), IF(B2 < 649,0.16, IF(B2 < 850, 0.16, 0)))
but it does not insert 125 value as its X with 125 I don't want to multiply if value above 650 and bellow 850. so please help me with this.
you can use arrayformula:
=ARRAYFORMULA(IF(B2:B<>"", IF((B2:B > 649)*(B2:B < 850), 125, B2:B* 0.16), ))
See if this works
=IF( AND (B2 > 649, B2 < 850), 125, B2* 0.16)

A cell containing a range of values and making calculations with that range

Is it possible to have a range of values in a cell so that Sheets understands it when calculating something?
Here's an example of the desired output:
A B C
1 Value Share Total sum
2 100.00 90-110% 90-110
Here, Total sum (C2) = A2 * B2 (so 100 * 90-110%), giving a range of 90-110.
However, I don't know how to insert this range of values into a cell without Sheets saying #VALUE!.
you will need to do it like this:
=REGEXREPLACE((A2*REGEXEXTRACT(B2, "\d+")%)&"-"&
A2*REGEXEXTRACT(B2, "-(\d+%)"), "\.$", )
for decimals:
=REGEXREPLACE((A40*REGEXEXTRACT(B40, "\d+.\d+|\d+")%)&"-"&
A40*REGEXEXTRACT(B40, "-(\d+.\d+%)|-(\d+%)"), "\.$", )

PSQL average with IF/ELSE

I have a psql table with columns including: year, value, ... and so on.
I want to do something like this:
select
CASE WHEN avg(value) >=0 then avg(value)
ELSE -999
END
from my_table
where year >= 2000 and year < 2005 and value >= 0
So I want my average to ignore any years that have negative value, but for cases where all years have negative value, I want to return -999.
This query runs but doesn't return -999 in the case where all values are negative.
If I've understood you correctly you're looking for something like:
select
coalesce(avg(value) filter (when value >= 0), -999)
from my_table
where year >= 2000 and year < 2005;
which returns -999 if all values are negative or there are zero rows that match the WHERE clause. avg returns null if it doesn't get any input rows.
The filter syntax works only on newer PostgreSQL versions. For older ones you must use avg(case when value >= 0 then value end).
you have given in the where clause value>=0 This will only compute the average of non negative values, hence you will never get the average of negative values
where year >= 2000 and year < 2005 and value >= 0
Guess what you are trying to do is this
select
CASE WHEN value >=0 then
(select avg(value) from t_table where value>=0)
ELSE -999
END
from table
where year >= 2000 and year < 2005;