i would like to divide two colums in Bigquery - google-bigquery

I would like is possible to divide the column A by the column B as column C, in Bigquery.
For example SELECT prix / surface as prixmcarre FROM 'appartement', is not possible in bigquery.
Error: No matching signature for operator / for argument types: STRING, STRING. Supported signatures: FLOAT64 / FLOAT64; NUMERIC / NUMERIC at [1:8]
In the Google Cloud Document, I don't see the simple solution.
Thank's for your help :)

You must be using strings that look like integers or floats. You need to cast them. For example:
select cast('10' as INT64) / cast('5' as INT64)

Related

I'm unable to cast numbers as float64 using bigquery, keep getting bad double value errors

I'm trying to extract numbers as substrings from between certain characters before casting them as float64 using BigQuery, but I'm getting a Bad Double Value error for some values. I tried using safe_cast to identify which values cause the error and they return as null (as expected) but I can't seem to figure out why these values can't be casted as float64 since they are in fact numbers. The only thing in common with the anomalies is that the first number extracted in a row is 0 but there are other values which do this and are casted fine.
This is an example of the string which numbers are extracted from: AOS-1545902(NCP)*0#84‬#475 which is object_text stored in a table named tr.
So in this case, the first number that is extracted is 0 from between the '*' and '#', the second is 84 from between '#' and '#', and the last one would be 475 after the '#'.
This is the query that I am using to extract the numbers and cast them to float64:
cast(substr(tr.object_text, strpos(tr.object_text,'*')+1, (strpos(tr.object_text,'#')-(strpos(tr.object_text,'*')+1))) as float64) AS FP_Share,
safe_cast(substr(tr.object_text, strpos(tr.object_text,'#')+1, (strpos(tr.object_text,'#')-(strpos(tr.object_text,'#')+1))) as float64) AS V_Share,
cast(substr(tr.object_text, strpos(tr.object_text,'#')+1) as float64) as Cust_Price,
From these, V_Share(the number between the '#' and '#') is the one that has these anomalies and when I extract the number without casting it to float64 using this query:
substr(tr.object_text, strpos(tr.object_text,'#')+1, (strpos(tr.object_text,'#')-(strpos(tr.object_text,'#')+1))) AS noCast_V_Share,
There are a total of 8 of these anomalies as you can see in the snippet below:
Results Snippet
Hoping someone could help me out with this!
Since you seem to have unprintable characters causing problems in your extraction, you could use REGEXP_EXTRACT to extract only the numbers (and possibly period if required) which should result in a valid conversion, something like;
CAST(
REGEXP_EXTRACT(
SUBSTR(tr.object_text,
STRPOS(tr.object_text,'#')+1,
(STRPOS(tr.object_text,'#')-(STRPOS(tr.object_text,'#')+1))),
'[0-9.]+'
) AS FLOAT64
) AS V_Share

Extracting value from curly bracket SQL

I am trying figure out how to extract a value from curly brackets in a column in Prestosql.
The field looks like,
rates
{"B":750}
{"B":1600}
{"B":900}
I want to extract the number values only in each bracket.
Also, if I want to divide that by 10 and then divide by 20 would that be easy to add into the query?
The rates column is of type map(varchar, bigint).
Since rates column is of type map(varchar, bigint). You can use Presto Map Functions and Operators on it. Examples:
SELECT rates['B'] FROM ... -- value under key "B"
SELECT map_values(rates) FROM ... -- all values in a map
See more in the Presto documentation.
Use something like this, where the regexp_extract function pulls out the number from your string, and the cast function converts this from a string to a number, which you can then go on to divide by 10 etc.
select cast(regexp_extract(rates, '\d+') as double) / 10
from my_table

Convert String to array<int> in Hive

I have a integer array represented with String. For example,
"[1,2,2,3]"
And the field type in Hive table is the array integer, I was wondering if there is any Hive build-in UDF that can cast the above string into the array integer.
Thanks
tl;dr I do not know of a Hive UDF that will do this for you, and casting on your own can be gross.
No, there isn't a UDF. As for building your own solution:
Casting to array[string] would be easy enough - just drop the square brackets using regexp_replace and split the resulting string on ,.
The problem is converting an array[string] to array[int] for arrays of arbitrary size. You can individually cast the array elements one by one:
hive> select id, my_array from array_table limit 3;
OK
10023307 ["0.20296966","0.17753501","-0.03543373"]
100308007 ["0.16155224","0.1945944","0.09167781"]
100384207 ["0.025892768","0.023214806","-0.003712816"]
hive> select array(cast(my_array[0] as double), cast(my_array[1] as double), cast(my_array[2] as double)) from array_table limit 3;
OK
[0.20296966,0.17753501,-0.03543373]
[0.16155224,0.1945944,0.09167781]
[0.025892768,0.023214806,-0.003712816]
but this approach only works because I know I have arrays of length 3.

How can I convert an IP address to an integer in BigQuery?

My primary goal is to filter out a range of IP addresses in IPv4 format by converting them to integers. So, for example, I'd like to filter an IP range from 192.168.1.1 to 192.168.1.100, but I first need to convert them to integers: 3232235777 to 3232235876.
Is there a way to do this in BigQuery (standard SQL)? I've looked at the BigQuery documentation but my combination of the NET.IP_FROM_STRING() and NET.IPV4_TO_INT64() functions keeps throwing this error: No matching signature for function NET.IPV4_TO_INT64 for argument types: STRING.
SELECT addr_str, ip_from_string, FORMAT("0x%X",
NET.IPV4_TO_INT64(ip_from_string)) AS ipv4_to_int64
FROM
(
SELECT addr_str, FORMAT("%T", NET.IP_FROM_STRING(addr_str)) AS ip_from_string
FROM UNNEST(['192.168.1.1','192.168.1.100']) AS addr_str
)
NET.IPV4_TO_INT64 function expects binary (BYTES) as an input, but you "accidentally" cast ip_from_string to string by applying FORMAT function.
Just remove it!
Why are you calling FORMAT on the result of NET.IP_FROM_STRING? NET.IP_FROM_STRING returns the IP address as bytes, and using that you can convert to an INT64 using NET.IPV4_TO_INT64, for example.
#standardSQL
SELECT
addr_str,
ip_from_string,
NET.IPV4_TO_INT64(ip_from_string) AS ipv4_to_int64
FROM (
SELECT
addr_str,
NET.IP_FROM_STRING(addr_str) AS ip_from_string
FROM UNNEST(['192.168.1.1','192.168.1.100']) AS addr_str
);
This answer covers the math of converting an IPv4 to an integer. Are there mathematical operators available to you in BigQuery?

BigQuery - function to handle hex string

Is there a way in BigQuery to convert a hex string to a decimal value?
Something like:
select hex("ff")
CAST now supports converting hexadecimal strings to INT64 or FLOAT64 values, even though it's not specified in their reference
Here's how you use it:
SELECT
CAST(columnA as FLOAT64) as float,
CAST(columnB as INT64) as int
FROM table
This should work, but it doesn't (I'm filing a feature request):
SELECT INTEGER('0xffff')
In the meantime, this does work:
SELECT FLOAT('0xffff')
255.0
For integer results:
SELECT INTEGER(FLOAT('0xffff'))
255
Looking into the query reference, I'd say no.
You have "HEX_STRING()" which does the opposite, but all the string to number functions seem to not take hex.