How to divide columns with zeros and nulls - sql

just a simple question but somehow I can't find an answer here.
I have two columns (A and B). Both contains numbers with zeros and null. I would like to get a division one by the other to get information about the ratio between each single row but I am getting ORA-01476.
I know the divisior is equal to zero but I would like to get in this row a number and not an error for whole query
A B
1 5
2 Null
3 0
NULL 3
0 4
4
I am using sql developer.

If you divide a number by zero you get an error, because the answer to such division is undefined. SQL, however, has a value for undefined: NULL. So make the result NULLinstead:
select a, b, case when b = 0 then null else a / b end as ratio
from mytable;
or
select a, b, a / case when b = 0 then null else b end as ratio
from mytable;
This is standard SQL and works in Oracle as well as in about every other RDBMS. Oracle also provides the function NULLIF as a shorter way to write the expression in the second query.

You can use nullif to return null instead of raising an error:
select A / nullif(B, 0) as division
from YourTable
If your numbers are stored as varchar, cast them to numbers before using them:
select to_number(A) / nullif(to_number(B), 0) as division
from YourTable

Related

SQL - concatenate values in columns but keep only first non-null value

I have the following table in Postgresql. The first 4 records are the base data and the others were generated with the ROLLUP function.
I want to add a column "grp_1" that will display the first non-null value of the columns grp1_l1, grp2_l2 and grp2_l3
I can get to the desired result by nesting 3 "case" functions using the SQL below, but my real table has 4 groups with each 8 to 10 columns (so a lot of nested "case" function).
sql:
SELECT grp1_l1, grp1_l2, grp1_l3, case when grp1_l1 is not null then grp1_l1 else case when grp1_l2 is not null then grp1_l2 else case when grp1_l3 is not null then grp1_l3 else null end end end as grp1, value
FROM public.query_test;
Is there a better and more scalable to handle this requirement ? Any suggestions are welcome.
The id will not always have 3 digits, that is just the case in my example here
Use coalesce() it's defined as "returns the first of its arguments that is not null" - which is exactly what you want.
coalesce(grp1_l1, grp1_l2, grp1_l3)

SUM function consider NULL on Oracle 12c

I was trying to get the nulls on doing a SUM() function on sql and I figured out that sql now consider nulls, so if a column has a different value and a null value the sum is good calculated. For example I have the column SUM_EXAMPLE with the different values: NULL - 3 - 4 - NULL - 6
So if I execute the next query: select sum(SUM_EXAMPLE) FROM TABLE T the result is 13 and not null.
And if I have another column SUM_COLUMNS only with NULL the result of select sum(SUM_COLUMNS) FROM TABLE T is NULL. It has been a change or a improvement of oracle so it does this function right without using functions like isNull or NVL?
Thank you
Aggregate functions (SUM, AVERAGE etc) generally ignore NULL values. Therefore if there is at least one non null value in the column, you will get a non null result.
If all values in a column being aggregated are null, there is pretty much no other option but to return null as the result.
Note that this is different to how scalar functions behave. For example: "Select x + y" will return null if either x, y or both are null. You will only get a non null result for this if both x and y have non-null values.

Divisor coming as null

I Have to find the percentage difference between two numbers.
i am using the formula A-B/(A+B/2)
I.e
select round( ( A-B)/(A+B/2)) from dual;
where a and b are two numbers. Now if a and b is 0 then the divisor comes as zero. In this case what can be done to avoid error ? what is the percentage difference between two equal numbers or 0 ?

Why does AVG(x) give a different answer than AVR((isnull(x,0))?

X is column with a mixture of numerical and null values.
I don't understand why treating null values as 0 changes the result of the AVG value.
Shouldn't it be like this,
(2 + null + 2) / 3 = 2 -- with null value
(2 + 0 + 2) / 3 = 2 -- no null value
If you have null value, it is not being taken into account, hence number of elements (your n) is not increased, whereas 0 is treated as a valid value.
For example if you have: 1,1,6,7,null,3,2 it will be total 20 divided by 6 (i.e. 3.33). However if you replace null with 0 it becomes 20 /7 (i.e. 2.86).
SQLFiddle demonstrating this behavior on MySQL server.
REFERENCE: Have a look in the documentation right below the table:
This section describes group (aggregate) functions that operate on sets of values. Unless otherwise stated, group functions ignore NULL values.

Finding even or odd ID values

I was working on a query today which required me to use the following to find all odd number ID values
(ID % 2) <> 0
Can anyone tell me what this is doing? It worked, which is great, but I'd like to know why.
ID % 2 is checking what the remainder is if you divide ID by 2. If you divide an even number by 2 it will always have a remainder of 0. Any other number (odd) will result in a non-zero value. Which is what is checking for.
For finding the even number we should use
select num from table where ( num % 2 ) = 0
As Below Doc specify
dividend % divisor
Returns the remainder of one number divided by another.
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/modulo-transact-sql#syntax
For Example
13 % 2 return 1
Next part is <> which denotes Not equals.
Therefor what your statement mean is
Remainder of ID when it divided by 2 not equals to 0
Be careful because this is not going to work in Oracle database. Same Expression will be like below.
MOD(ID, 2) <> 0
ID % 2 reduces all integer (monetary and numeric are allowed, too) numbers to 0 and 1 effectively.
Read about the modulo operator in the manual.
In oracle,
select num from table where MOD (num, 2) = 0;
dividend % divisor
Dividend is the numeric expression to divide. Dividend must be any expression of integer data type in sql server.
Divisor is the numeric expression to divide the dividend. Divisor must be expression of integer data type except in sql server.
SELECT 15 % 2
Output
1
Dividend = 15
Divisor = 2
Let's say you wanted to query
Query a list of CITY names from STATION with even ID numbers only.
Schema structure for STATION:
ID Number
CITY varchar
STATE varchar
select CITY from STATION as st where st.id % 2 = 0
Will fetch the even set of records
In order to fetch the odd records with Id as odd number.
select CITY from STATION as st where st.id % 2 <> 0
% function reduces the value to either 0 or 1
It's taking the ID , dividing it by 2 and checking if the remainder is not zero; meaning, it's an odd ID.
<> means not equal. however, in some versions of SQL, you can write !=