BigQuery array intersect [duplicate] - google-bigquery

This question already has an answer here:
BigQuery check for array overlap
(1 answer)
Closed 5 years ago.
I have an array of values I'd like to filter results for if an array column contains any of these values, is there an easy way to perform an intersect in BigQuery using the standard SQL language?

This should give you the general direction:
SELECT ...
WHERE EXISTS(SELECT 1 FROM UNNEST(array_column) a WHERE a IN UNNEST(array_values))

Related

Select a range of numbers not in a table in SQL [duplicate]

This question already has answers here:
What is the best way to create and populate a numbers table?
(12 answers)
Closed 4 years ago.
I am wondering if it is possible to get a query that will take a range of numbers, in this case 8 to 17, compare it against a field in a table and remove the ones that do appear in the table and return the rest?
I assume the peusdo code would look something like
Select nums from range(8-17) where nums not in (select column from table)
Is this possible at all?
Edit
To clarify my question.
In table I might have the following:
Intnumber
9
10
16
I would like to have the numbers between 8-17 that do not appear in this table, so 8,11,12,13,14,15,17
Kind regards
Matt
select nums from table where nums not between 8 and 17;

Its like transpose rows to columns in DB2 while we can't use aggregation [duplicate]

This question already has answers here:
Coalescing values in a column over a partition
(3 answers)
Closed 4 years ago.
I have a table as below
Element Value
State GA
State CA
State IL
And so on ....
I need to transpose above output as follows. It's similar to transposing, but little different as I need comma separated columns.
Element Value
State GA,CA,IL
Can someone help me to achieve above result in db2?
I got the answer with the help of #mustaccio
used listagg function..

BigQuery split column and get count of count each substring [duplicate]

This question already has answers here:
How can I compute TF/IDF with SQL (BigQuery)
(3 answers)
Closed 4 years ago.
In BigQuery I would like to create a query to count the occurrence of words in a comments field and group by a count of each occurrence. This would me get a sense of what words were used more than others and get a sense of user behavior and moods. Pretty new to bigquery so any ideas will be helpful.
What I ended up doing was using the split function...
SELECT
COUNT(JJ) AS STUFF, JJ
FROM
(SELECT SPLIT(text, ' ') AS JJ FROM [bigquery-public-
data:hacker_news.comments] LIMIT 1000 )
GROUP BY JJ
ORDER BY STUFF DESC
LIMIT 5
Obviously it can be manipulated more by using replace to remove other characters before splitting.

Division Operator [duplicate]

This question already has answers here:
SQL Server round after division
(2 answers)
Closed 5 years ago.
Is there any way to return 3 as a result of ((5168/2000) .
I'm using CEILING(5168/2000) to delete rows. Its returning 2. But I want 3.
So that loop execute for 3 times.
Is there any way to return 3 as a result of (5168/2000)
Yes, make sure you're diving floating point numbers not integers
CEILING(5168.0/2000)
One way to achive that, aside from hardcoding the .0 as I have above, is to cast your integer to an appropriate type, for example:
CEILING(CAST(5168 AS FLOAT)/2000)
or muultiply it by a decimal
CEILING((1.0 * 5168)/2000)

sql max(val1,val2)? [duplicate]

This question already has answers here:
How to get min/max of two integers in Postgres/SQL?
(2 answers)
Closed 6 years ago.
is there some way to determine the max value out of two values in SQL?
I use the mod function:
MOD(cnt, cnt/100)
This yields a division by 0 error when cnt is smaller than 100. I therefore would like something like this:
MOD(cnt, MAX(cnt/100, 1))
You can use greatest
SELECT greatest(a, b, c) FROM your_table;