BigQuery: Mod operator returning negative result when dividing by positive integer - google-bigquery

I have a table with a couple of fields. The first field is the userId. I am using the hash function to shard the data by userId.
I am running the following query:
SELECT userId, HASH(userId) as hashedId, HASH(userId) % 3 as hashedIdMod3
FROM mydataset.mytable LIMIT 1000
So for example:
-5655326518438853587 % 3 ==> -1 when it should be 2
HASH(27315207816077732041734307321022553299) is -3139846784539570547 and the remainder is -2 when divided by 3 when it should be 1
So, how can the remainder be negative when dividing by a positive integer?

mod of a negative number is negative in SQL (and c++, java, etc). So you'll want to use ABS() -- as in:
SELECT userId,
HASH(userId) as hashedId,
ABS(HASH(userId) % 3) as hashedIdMod3
FROM mydataset.mytable LIMIT 1000

Related

After inserting to the table, digits after decimal point changes to 0

While SELECT'ing columns in Vertica, it shows normal numeric values:
SELECT nvl2(exposure_time_ms, ROUND(exposure_time_ms / 1000, CASE WHEN exposure_time_ms < 10000 THEN 1 ELSE 0 END), 0) :: numeric(12,1) AS exposure_seconds
1
But when I am inserting the same thing to the table, which has column 'exposure seconds' type NUMERIC(12,1), it changes all digits after the decimal point to 0:
2
Presumably, this is because the nvl2() calculation produces an integer. So, try this:
SELECT nvl2(exposure_time_ms,
ROUND(exposure_time_ms / 1000.0,
CASE WHEN exposure_time_ms < 10000 THEN 1 ELSE 0 END
), 0
)::numeric(12,1) AS exposure_seconds
I will say that it really doesn't make sense to round the values based on the magnitude. You should store the value "as is" and use rounding for presentation -- if it is really needed.

Redshift division result does not include decimals

I'm trying to do something really quite basic to calculate a kind of percentage between two columns in Redshift. However, when I run the query with an example the result is simply zero because the decimals are not being covered.
code:
select 1701 / 84936;
Output:
I tried :
select cast(1701 / 84936 as numeric (10,10));
but the result was 0.0000000000.
How could I solve this silly thing?
It is integer division. Make sure that at least one argument is: NUMERIC(accurate data type)/FLOAT(caution: it's approximate data type):
/ division (integer division truncates the result)
select 1701.0 / 84936;
-- or
SELECT 1.0 * 1701 / 84936;
-- or
SELECT CAST(1701 AS NUMERIC(10,4))/84936;
DBFiddle Demo
When mixing data types the order counts
Note that the order of the elements in a math expression counts for the data type of the result.
Let's assume that we intend to calculate the percentage unit_sales/total_sales where both columns (or numbers) are integers.
See and try with this code here.
-- Some dummy table
drop table if exists sales;
create table sales as
select 3 as unit_sales, 9 as total_sales;
-- The calculations
select
unit_sales/total_sales*100, --> 0 (integer)
unit_sales/total_sales*100.0, --> 0.0 (float)
100.0*unit_sales/total_sales --> 33.3 (float and expected result)
from sales;
The output
0 | 0.0 | 33.33
The first column is 0 (integer) because of 3/9=0 in an integer division.
The second column is 0.0 because SQL first got the integer 0 (3/9), and later, SQL converts it to float in order to perform the multiplication by 100.0.
The expected result.
The non-integer 100.0 at the beginning of the expression force a non-integer calculation.

Counting the number of digits in column

Here is my code
select len(cast(code as float)),code
from tbl1
where code is not null
and this is the output:
I want a count of digits in the code column.
I don't understand why the last one is counted as 12 and not 8?
Cast it as an int instead:
select len(cast(code as int)), code
from tbl1
where code is not null;
Presumably, some sort of decimal values are getting counted.
Get the number's power of 10 and add 1. This works either if ints or reals to count the number of digits of the whole number part (note using LOG10 only works on positive numbers so I have applied ABS to get around this issue, may not be required for your data):
SELECT code, CASE WHEN Number = 0 THEN 1
ELSE FLOOR(LOG10(ABS(code))) + 1 AS NDigits
FROM tbl1

Select random row with different probability - SQL

I want select random row with different probability column based:
ID Type
Bike 1
Moto 1
Asus 2
Car 1
Apple 2
John 3
If i do this i will have random probability:
select top 1 * from Items order by newid()
I want John (type 3) has 70% probability to get, and 5% for type 1 and 25% for type 2.
I would use the RAND() function instead of NEWID().
Using RAND(), we can generate a random number between 1 and 100, and then use a CASE statement to select a type based on the number randomly generated.
According to MSDN:
RAND() returns a pseudo-random float value from 0 through 1,
exclusive
Meaning that multiplying RAND() by 100 will give us a number from 0 to 99. Adding 1 changes the range to 1 to 100.
If after selecting which type to return, you want to randomly select a record from that type, you can then add a SELECT TOP 1... ORDER BY NEWID() to get a random record of that type:
DECLARE #Random INT
SET #Random = (RAND() * 100) + 1
SELECT TOP 1 ID, Type
FROM Items
WHERE Type = CASE
WHEN #Random > 30 THEN 3
WHEN #Random BETWEEN 6 AND 30 THEN 2
ELSE 1
END
ORDER BY NEWID()
See it here... run it a few times to see that the results match the probabilities.
You mean 5% probability for entire type=1 group, or you want every record of type=1 to have 5% probability of being selected?
If it's second option, then you have 70+15+50=135 = no way you can do this.
If it's first option, then you'll have to make 2 draws - first for a type, and then for a row in this type.

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 !=