HQL Calculations in 'case when' not returning decimal - decimal

In an HQL statement I am calculating metric_ty in my select statement and when flag_ty is 'Zero' then metric_ly takes the value 0 otherwise metric_ly is calculated.
My problem is that metric_ty gives back decimal values(eg: 54.32) while metric_ly does not(eg:54.00).
I want to know why this happens and how can i return decimal values inside an case-when loop?
select (p.total_revenue/p.total_units) as metric_ty,
(case
when flag_ty = 'Zero' THEN 0 ELSE
(total_revenue_ly/total_units_ly) END)
as metric_ly
from Com p
Is it possible to use case-when statements and get exact numbers and not rounding off?
All suggestions are most welcome

Data type is chosen based to first scalar expression (0). Problem can be solved by using 0.0 instead of 0:
SELECT (p.total_revenue/p.total_units) AS metric_ty,
(CASE
WHEN flag_ty = 'Zero' THEN 0.0
ELSE (total_revenue_ly/total_units_ly) END)
END metric_ly
FROM Com p

Related

Why I am getting FALSE for "=FALSE<=100" in excel

Hi I am Priya from India,
I am trying to get the logic in excel formula
=FALSE<=100
False will be considered as 0. if it is 0<=100 then expected output is TRUE.
But we are getting FALSE in the Microsoft Excel.
Why I am not getting True?
TRUE and FALSE can't be treated directly as numbers on Excel. However, they can be implicitly converted to numbers:
=(FALSE + 0) <= 100
Adding 0 to them does it.
The formula you mentioned in the comment, even though it seems to return a correct result - (10> = 50 AND 10 = <- 100) == FALSE, is basically incorrect, because excel calculates it as follows:
=(10>=50 & 10<=100)
=(10>="5010"<=100)
=(FALSE<=100)
= FALSE
If you want the numbers to be compared, you can use the following options:
Use the AND function:
=AND((10>=50),(10<=100))
Multiply the comparisons and check if the result is greater than zero
=(10>=50)*(10<=100)>0

how to convert decimal to binary by using repeated division in python

how to convert decimal to binary by using repeated division in python?
i know i have to use a while loop, and use modulus sign and others {%} and {//} to do this...but i need some kind of example for me to understand how its done so i can understand completely.
CORRECT ME, if I'm wrong:
number = int(input("Enter a numberto convert into binary: "))
result = ""
while number != 0:
remainder = number % 2 # gives the exact remainder
times = number // 2
result = str(remainder) + result
print("The binary representation is", result)
break
Thank You
Making a "break" without any condition, makes the loop useless, so the code only executes once no matter what.
-
If you don't need to keep the original number, you can change "number" as you go.
If you do need to keep the original number, you can make a different variable like "times".
You seem to have mixed these two scenarios together.
-
If you want to print all the steps, the print will be inside the loop so it prints multiple times.
If you only want to print the final result, then the print goes outside the loop.
while number != 0:
remainder = number % 2 # gives the exact remainder
number = number // 2
result = str(remainder) + result
print("The binary representation is", result)
-
The concatenation line:
Putting the print inside the loop might help you see how it works.
we can make an example:
the value in result might be "11010" (a string, with quotes)
the value in remainder might be 0 (an integer, no quotes)
str(remainder) turns the remainder into a string = "0" instead of 0
So when we look at the assignment statement:
result = str(remainder) + result
The right side of the assignment operator = is evaulated first.
The right side of the = is
str(remainder) + result
which, as we went over above has the values:
"0" + "11010"
This is string concatenation. It just puts one string on the end of the other one. The result is:
"0 11010"
"011010"
That is the value evaluated on the right side of the assignment statement.
result = "011010"
Now that is the value of result.
B_Number = 0
cnt = 0
while (N != 0):
rem = N % 2
c = pow(10, cnt)
B_Number += rem * c
N //= 2
# Count used to store exponent value
cnt += 1
return B_Number

How to match a floating point number when reading a string

How can I match floating point numbers like 1.234 or that use the "E notation" like 1.23e04 when dealing with strings?
As an example, let's say that I would like to read numbers from a data file like the following:
0.0 1.295e-03
0.1 1.276e-03
0.2 1.261e-03
0.3 1.247e-03
0.4 1.232e-03
0.5 1.218e-03
At the moment I wrote my own function to convert each line in the numbers it contains, but it's not very elegant and not portable at all: data files with a different "layout" will give errors.
Here is a simple example that reads the data file already presented and prints to screen the numbers:
function read_line(str)
local a, b, c, d, e = str:match(
"^%s*(%d+)%.(%d+)%s+(%d+)%.(%d+)[Ee]%-*(%d+)")
if str:match("%-") then
e = -tonumber(e)
end
local v1 = a + .1*b
local v2 = (c + .001*d) * 10^e
return v1, v2
end
for line in io.lines("data.txt") do
print(read_line(line))
end
and this gives as a result:
0 0.001295
0.1 0.001276
0.2 0.001261
0.3 0.001247
0.4 0.001232
0.5 0.001218
This is indeed the result I want to achieve, but is there a more elegant and general way to deal with this problem?
Note: Data files can have more than two columns of numbers and can have both the floating point representation and the "E notation".
Assuming every line contains only whitespace separated numbers, you can let tonumber do the heavy work instead of matching numbers manually:
function split_number(str)
local t = {}
for n in str:gmatch("%S+") do
table.insert(t, tonumber(n))
end
return table.unpack(t)
end
for line in io.lines("data.txt") do
print(split_number(line))
end
Lua can read numbers directly:
f=assert(io.open("data.txt"))
while true do
local a,b=f:read("*n","*n")
if b==nil then break end
print(a,b)
end
f:close()
This works for on the lua REPL.
a = tonumber('4534.432')
b = tonumber('4534.432')
a==b
So your answer is simply to use tonumber.

Recognize relevant string information by checking the first characters

I have a table with 2 columns. In column 1, I have a string information, in column 2, I have a logical index
%% Tables and their use
T={'A2P3';'A2P3';'A2P3';'A2P3 with (extra1)';'A2P3 with (extra1) and (extra 2)';'A2P3 with (extra1)';'B2P3';'B2P3';'B2P3';'B2P3 with (extra 1)';'A2P3'};
a={1 1 0 1 1 0 1 1 0 1 1 }
T(:,2)=num2cell(1);
T(3,2)=num2cell(0);
T(6,2)=num2cell(0);
T(9,2)=num2cell(0);
T=table(T(:,1),T(:,2));
class(T.Var1);
class(T.Var2);
T.Var1=categorical(T.Var1)
T.Var2=cell2mat(T.Var2)
class(T.Var1);
class(T.Var2);
if T.Var1=='A2P3' & T.Var2==1
disp 'go on'
else
disp 'change something'
end
UPDATES:
I will update this section as soon as I know how to copy my workspace into a code format
** still don't know how to do that but here it goes
*** why working with tables is a double edged sword (but still cool): I have to be very aware of the class inside the table to refer to it in an if else construct, here I had to convert two columns to categorical and to double from cell to make it work...
Here is what my data looks like:
I want to have this:
if T.Var1=='A2P3*************************' & T.Var2==1
disp 'go on'
else
disp 'change something'
end
I manage to tell matlab to do as i wish, but the whole point of this post is: how do i tell matlab to ignore what comes after A2P3 in the string, where the string length is variable? because otherwise it would be very tiring to look up every single piece of string information left on A2P3 (and on B2P3 etc) just to say thay.
How do I do that?
Assuming you are working with T (cell array) as listed in your code, you may use this code to detect the successful matches -
%%// Slightly different than yours
T={'A2P3';'NotA2P3';'A2P3';'A2P3 with (extra1)';'A2P3 with (extra1) and (extra 2)';'A2P3 with (extra1)';'B2P3';'B2P3';'NotA2P3';'B2P3 with (extra 1)';'A2P3'};
a={1 1 0 1 1 0 1 1 0 1 1 }
T(:,2)=num2cell(1);
T(3,2)=num2cell(0);
T(6,2)=num2cell(0);
T(9,2)=num2cell(0);
%%// Get the comparison results
col1_comps = ismember(char(T(:,1)),'A2P3') | ismember(char(T(:,1)),'B2P3');
comparisons = ismember(col1_comps(:,1:4),[1 1 1 1],'rows').*cell2mat(T(:,2))
One quick solution would be to make a function that takes 2 strings and checks whether the first one starts with the second one.
Later Edit:
The function will look like this:
for i = 0, i < second string's length, i = i + 1
if the first string's character at index i doesn't equal the second string's character at index i
return false
after the for, return true
This assuming the second character's lenght is always smaller the first's. Otherwise, return the function with the arguments swapped.

Matlab - How do I compare two strings letter by letter?

Essentially, I have two strings of equal length, let's say 'AGGTCT' and 'AGGCCT' for examples sake. I want to compare them position by position and get a readout of when they do not match. So here I would hope to get 1 out because there is only 1 position where they do not match at position 4. If anyone has ideas for the positional comparison code that would help me a lot to get started.
Thank you!!
Use the following syntax to get the number of dissimilar characters for strings of equal size:
sum( str1 ~= str2 )
If you want to be case insensitive, use:
sum( lower(str1) ~= lower(str2) )
The expression str1 ~= str2 performs char-by-char comparison of the two strings, yielding a logical vector of the same size as the strings, with true where they mismatch (using ~=) and false where they match. To get your result simply sum the number of true values (mismatches).
EDIT: if you want to count the number of matching chars you can:
Use "equal to" == operator (instead of "not-equal to" ~= operator):
sum( str1 == str2 )
Subtract the number of mismatch, from the total number:
numel(str1) - sum( str1 ~= str2 )
You can compare all the element of the string:
r = all(seq1 == seq2)
This will compare char by char and return true if all the element in the resulting array are true. If the strings can have different sizes you may want to compare the sizes first. An alternative is
r = any(seq1 ~= seq2)
Another solution is to use strcmp:
r = strcmp(seq1, seq2)
Just would like to point out that you are asking to calculate the hamming distance (as you ask for alternatives - the article contains links to some). This is already discussed here. In short the builtin command pdist can do it.

Resources