google bigquery selecting rows with multiple values in repeating field - google-bigquery

Lets say I have a schema:
key STRING NULLABLE
values INTEGER REPEATED
Now, please note that second column is a repeated field of integers,
Lets say the data is something like:
key:'abc'
values: 1 2 3 (3 separate values, same for below values)
key:'def'
values: 1 2 5
key:'ghi'
values: 1 6 9
And here I wish to find out keys which has values 1 and 2 ? Expecting 'abc' and 'def' as result set.
Looking for a query for this. I want an 'and' ('in' does not work here). I need those both values to be present for any key to return as result.

SELECT
key,
SUM(values = 1 or values = 2) WITHIN RECORD AS check
FROM yourtable
HAVING check = 2

Related

Generate random records from the table tblFruit based on the field Type

I will need your help to generate random records from the table tblFruit based on the field Type (without no duplication)
As per the above table.
There are 4 type of fruit number 1,2,3,4
I want to generate x records dynamically from the table tblFruit (e.g 7 records).
Let say I need to get 7 random record of fruit .
My result should contains fruit of the different types. However, we need to ensure that the result contains only 7 records.
i.e
2 records of type 1,
2 records of type 2,
2 records of type 3,
1 records of type 4
e.g
Note: If i want to generate 10 records (without no duplication),
then i will get 2 records of each type and the two remaining records randomly of any type.
Much grateful for your help.
I might suggest:
select top (7) f.*
from tblfruit f
order by row_number() over (partition by type order by newid());
This will actually produce a result with approximately the same number of rows of each type (well, off by 1), but that meets your needs.

I need a query in postgres that returns the length of arrays but without taking empty values into consideration

so given the table:
id | names
===============
1 {John, , Wayne}
2 {Luke, Harold, }
3 {Bill}
4 {Will, , }
They don't have a standard and some values may come empty ( for example {Will, , }).
I tried:
SELECT array_length(names, 1)
FROM nameTable
But I get this:
names
======
3
3
1
3
and I want it to return:
names
======
2
2
1
1
So I need something which gives me the length only of the populated fields (empty spaces like ' ') shouldn't be counted.
You can remove the NULL values and then count:
array_length(array_remove(names, NULL), 1)
For one-dimensional arrays, I find that cardinality() is convenient:
cardinality(array_remove(names, NULL))

Issue with creating oracle query

I am having problem coming up with following query.
id| prop_id| obj_id| value|
1 7 2 1
2 8 2 1
4 7 5 7
5 8 5 12
Input parameters to the query are:
Collection of (prop_id, value) pairs
We must select records whose prop_id and values match input parameters: but there is also one constrain, the obj_id of resulting records must be same. If it is not same, result must be empty. Otherwise it must return obj_id.
Example:
Let's say input parameters are: (7,1) and (8,12).
Now there are two records with such values: first and last.
However, obj_id of first record is 2 and obj_id of second record is 5. Hence, the result must be empty because 2!=5.
Another example:
Let's say input parameters are: (7,7) and (8,12).
Now there are two records with such values: last two records.
obj_id of first is 5 and obj_id of second record is also 5. Hence, the result must be 5.
Try this
select obj_id from table
where (prop_id=7 and value=7) or (prop_id=8 and value=12)
group by obj_id having count(*)=2 and count(distinct object_id)=1

Searching for a number in a database column where column contains series of numbers seperated by a delimeter '"&" in SQLite

My table structure is as follows :
id category
1 1&2&3
2 18&2&1
3 11
4 1&11
5 3&1
6 1
My Question: I need a sql query which generates the result set as follows when the user searched category is 1
id category
1 1&2&3
2 18&2&1
4 1&11
5 3&1
6 1
but i am getting all the results not the expected one
I have tried regexp and like operators but no success.
select * from mytable where category like '%1%'
select * from mytable where category regexp '([.]*)(1)(.*)'
I really dont know about regexp I just found it.
so please help me out.
For matching a list item separated by &, use:
SELECT * FROM mytable WHERE '&'||category||'&' LIKE '%&1&%';
this will match entire item (ie, only 1, not 11, ...), whether it is at list beginning, middle or end.

SQL Select where id is in `column`

I have a column that has multiple numbers separated by a comma. Example for a row:
`numbers`:
1,2,6,66,4,9
I want to make a query that will select the row only if the number 6 (for example) is in the column numbers.
I cant use LIKE because if there is 66 it'll work too.
You can use like. Concatenate the field separators at the beginning and end of the list and then use like. Here is the SQL Server sytnax:
where ','+numbers+',' like '%,'+'6'+',%'
SQL Server uses + for string concatenation. Other databases use || or the concat() function.
You should change your database to rather have a new table that joins numbers with the row of your current table. So if your row looks like this:
id numbers
1 1,2,6,66,4,9
You would have a new table that joins those values like so
row_id number
1 1
1 2
1 6
1 66
1 4
1 9
Then you can search for the number 6 in the number column and get the row_id