I want to extract a value out of an array of numerical values with index position in PostgreSQL.
My array is like {0.10,0.20,0.30}, type is numeric[], array position is an integer calculated with array_position. Syntax will be SELECT myarray[array_position];, but when I try (for example) :
SELECT'{0.10,0.20,0.30}'::numeric[][1];
It returned me the entire array.
How to extract a value out of an array with index position in PostgreSQL?
In your example [1] is the part of array definition: https://www.postgresql.org/docs/current/arrays.html#ARRAYS-DECLARATION
However, the current implementation ignores any supplied array size limits, i.e., the behavior is the same as for arrays of unspecified length.
Use parentheses to split the definition and reference:
SELECT ('{0.10,0.20,0.30}'::numeric[])[1];
Related
I have JSON array in text type field in DB, and I want to check whether there are some elements in the array, and in that case, add some elements to this array and save it back to DB. The main issue I am having is that this text field looks like this:
["elem1","elem2","elem3"]
and I cannot figure out how to work with those double-quotes.
When I tried to_json it resulted in:
"[\"elem1\",\"elem2\",\"elem3\"]"
When I tried string_to_array:
{"[\"elem1\"","\"elem2\"","\"elem3\"]"}
I just need something like that:
['elem1', 'elem2', 'elem3']
To get a JSON array, simply cast it into json directly:
demo:db<>fiddle
SELECT '["elem1","elem2","elem3"]'::json
If you need a simple array (without JSON), you need to expand the elements of the JSON array (json_array_elements_text()) and aggregate them afterwards (array_agg()):
SELECT
array_agg(elems)
FROM mytable,
json_array_elements_text(mydata::json) AS elems
I have an array of strings as one of my columns, and I want to sort the result by the first element of the array. This is what I have tried:
SELECT * FROM items ORDER BY aliases[0];
This did not work. How may this be accomplished?
Arrays in Postgres are indexed beginning at position 1, not 0. From the documentation:
By default PostgreSQL uses a one-based numbering convention for arrays, that is, an array of n elements starts with array[1] and ends with array[n].
With this in mind, try the following query:
SELECT * FROM items ORDER BY aliases[1];
I want to select a single column in mgo which is array of strings currently using this returns a empty array
classifiedColl.Find(nil).Distinct("Tags", &tags)
and i want to group it how do i do it. The value i want to group is array of strings.
I have a problem with swift. I want to search an array of strings consists of 3 different elements filling this array in certain manner. Next i want to search the array for a subset of 3 particular strings next to each other and return their indexes. Is that a special array method for this?
Copied from comment:
Assumed i have an Int array like [1,2,1,3,2]. I want to search it for a subarray [1,2,1] and return indexes of those elements. Should i use findwithPredicate method?
I would just iterate the Array checking for matches.
Check the items in the array in sequence comparing the current item with the first item of the subarray. If it does not match move on. If it does start comparing the rest of the subarray, if it fails go bad to scanning the array.
I am trying to calculate the average of millions of records with NumberLong type in Mongo.
However aggregate and $avg doesn't work because of the sizes.
Any good approach to solve it?
You can use MapReduce for this.
Your map function would take each document and emit an object with two fields: one field value with the value you want to average and one field count with a value of 1.
Your reduce function would then sum up both the field count and the field value of all objects passed to it, returning one object representing how many documents were summarized and what their sum is.
Your finalize function would then divide the value by the count of the resulting object and return this number.
The second MapReduce example in the official documentation is very close to your use-case, you should be able to use it as a reference. The only difference is that you only want one average value, not separate ones for subsets of your collection, so you would replace key with a constant value.