How to find string values in text array using SQL query.
Suppose I have:
id location
1 {Moscow,New york}
2 {Mumbai}
3 {California,Texas}
I want to find id whose location is Moscow.I used:
select id from table where location in ('Moscow'); but get error:
ERROR: malformed array literal: "Moscow"
LINE 1: select id from table where location in ('Moscow');
DETAIL: Array value must start with "{" or dimension information.
I am using Postgres.
For DataType=Array, you can use the method=Any.
select id from table where 'Moscow' = Any(location)
As the document which describes DataType=Array:
8.14.5. Searching in Arrays
To search for a value in an array, each value must be checked. This
can be done manually, if you know the size of the array.
or use the method = Any:
9.21.3. ANY/SOME (array)
expression operator ANY (array expression) expression operator SOME
(array expression) The right-hand side is a parenthesized expression,
which must yield an array value. The left-hand expression is evaluated
and compared to each element of the array using the given operator,
which must yield a Boolean result. The result of ANY is "true" if any
true result is obtained. The result is "false" if no true result is
found (including the case where the array has zero elements).
For Searching in Array DataType you can use the ANY()
SELECT id FROM table WHERE 'Moscow' = ANY(location);
Live Demo
http://sqlfiddle.com/#!17/a6c3a/2
select id from demo where location like '%Moscow'%'
Related
I have a column that contains an array of strings. I try to select the items which have a certain word in that column but I get this error:
function contains(character varying[], unknown) does not exist
This is my select query:
SELECT "schedule", COUNT("schedule") AS "count"
FROM "members" AS "member"
WHERE contains("member"."facility_id", 'DEMO')
GROUP BY "schedule";
If you want to test if an array of strings contains a specific element you need to use the contains operator which is #> but only works with arrays on both sides
WHERE member.facility_id #> array['DEMO']
To test for a single value, you can also use the any operator:
WHERE 'DEMO' = ANY(member.facility_id)
I have a dataframe with column of array (or list) with each element being a map of String, complex data type (meaning --String, nested map, list etc; in a way you may assume column data type is similar to List[Map[String,AnyRef]])
now i want to query on this table like..
select * from the tableX where column.<any of the array element>['someArbitaryKey'] in ('a','b','c')
I am not sure how to represent <any of the array element> in the spark SQL. Need help.
The idea is to transform the list of maps into a list of booleans, where each boolean indicates if the respective map contains the wanted key (k2 in the code below). After that all we have to check if the boolean array contains at least one true element.
select * from tableX where array_contains(transform(col1, map->map_contains_key(map,'k2')), true)
I have assumed that the name of the column holding the list of maps is col1.
The second parameter of the transform function could be replaced by any expression that returns a boolean value. In this example map_contains_key is used, but any check resulting in a boolean value would work.
A bit unrelated: I believe that the data type of the map cannot be Map[String,AnyRef] as there is no encoder for AnyRef available.
I have a jsonarray as given below
[{"key1":10},{"key1":20},{"key1":30}]
I want to get all the json objects with a specified condition. say get all the json objects with key1 less than 25. so my sql query should return this list
[{"key1":10},{"key1":20}]
what is the resultant SQL query for this.
step-by-step demo:db<>fiddle
SELECT
json_agg(elements) -- 3
FROM mytable,
json_array_elements(mydata) as elements -- 1
WHERE (elements ->> 'key1')::int < 25 -- 2
Extract the JSON array: Each element is now in an own record
Filter by the value. Notice, that ->> returns a type text, so you need to cast it into type int in your case
Reaggregate the remaining elements into the new array
If you are using Postgres 12 or later you can use a JSON path function:
select jsonb_path_query_array(the_column, '$[*] ? (#.key1 <= 25)')
from the_table
Online example
I have a column which is of an array type. I want to use the where condition in my script, but am unable to. The unnest formula is too complex to use and I want to keep it simple here.
I have 4 columns. One of them is called box_number. It can have an array of multiple numbers. I want to search for rows where box_number contains 123.
select
*
from BOX_TABLE
where box_number is {123}
ERROR: syntax error at or near "{"
SELECT * FROM BOX_TABLE WHERE 123 = ANY (box_number);
You check that at least one value inside the column is 123.
To test for equality, try
WHERE box_number = ARRAY[123]
To test if the array contains your value, use the “contains” operator &&:
WHERE box_number && ARRAY[123]
I would like to create a function on postgresql that receives an array of bigint (record ids) and to use the received information on a query using the "in" condition.
I know that I could simply fo the query by my self, but the point over here is that I'm going to create that function that will do some other validations and processes.
The source that I was tring to use was something like this:
CREATE OR REPLACE FUNCTION func_test(VARIADIC arr bigint[])
RETURNS TABLE(record_id bigint,parent_id bigint)
AS $$ SELECT s.record_id, s.parent_id FROM TABLE s WHERE s.column in ($1);
$$ LANGUAGE SQL;
Using the above code I receive the following error:
ERROR: operator does not exist: bigint = bigint[]
LINE 3: ...ECT s.record_id, s.parent_id FROM TABLE s WHERE s.column in ($1)
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
How can I fix this?
IN doesn't work with arrays the way you think it does. IN wants to see either a list of values
expression IN (value [, ...])
or a subquery:
expression IN (subquery)
A single array will satisfy the first one but that form of IN will compare expression against each value using the equality operator (=); but, as the error message tells you, there is no equality operator that can compare a bigint with a bigint[].
You're looking for ANY:
9.23.3. ANY/SOME (array)
expression operator ANY (array expression)
expression operator SOME (array expression)
The right-hand side is a parenthesized expression, which must yield an array value. The left-hand expression is evaluated and compared to each element of the array using the given operator, which must yield a Boolean result. The result of ANY is "true" if any true result is obtained. The result is "false" if no true result is found (including the case where the array has zero elements).
So you want to say:
WHERE s.column = any ($1)
Also, you're not using the argument's name so you don't need to give it one, just this:
CREATE OR REPLACE FUNCTION func_test(VARIADIC bigint[]) ...
will be sufficient. You can leave the name there if you want, it won't hurt anything.