Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL: get count of occurrences of specified element in array

I need to calculate the count of occurrences of specified element in array, something like:

elem_occurrences_count(ARRAY[a,b,c,a,a], a) = 3

elem_occurrences_count(ARRAY[a,b,c], d) = 0

Is there any function in PostgreSQL that can be used to solve the problem? Any help is appreciated.

like image 859
Stanislav Belyakov Avatar asked Oct 23 '14 07:10

Stanislav Belyakov


People also ask

How do you count the occurrences of a particular element in an array?

To count the occurrences of each element in an array:Declare a variable that stores an empty object. Use the for...of loop to iterate over the array. On each iteration, increment the count for the current element if it exists or initialize the count to 1 .

What is count (*) in PostgreSQL?

1) COUNT(*) You can use the PostgreSQL COUNT(*) function along with a SELECT statement to return the total number of rows in a table including the NULL values as well as the duplicates.

What is Unnest in PostgreSQL?

Unnest function generates a table structure of an array in PostgreSQL. Unnest array function is beneficial in PostgreSQL for expanding the array into the set of values or converting the array into the structure of the rows. PostgreSQL offers unnest() function.

How do I count characters in PostgreSQL?

The PostgreSQL char_length function or character_length function is used to count the number of characters in a specified string. The CHARACTER_LENGTH() function is similar to CHAR_LENGTH() function. A string whose length is to be retrieved.


3 Answers

You will need to unnest the array and then count the occurrences.

with elements (element) as (
   select unnest(ARRAY['a','b','c','a','a'])
)
select count(*)
from elements
where element = 'a';

This can easily be embedded into a function:

create or replace function count_elements(elements text[], to_find text)
  returns bigint
as
$body$
  select count(*) 
  from unnest(elements) element 
  where element =  to_find;
$body$
language sql;

Update

Since Postgres 9.5 this can also be done using array_positions() which returns an array of positions where an element was found. The length of that array is the number of occurrences:

select cardinality(array_positions(ARRAY['a','b','c','a','a'], 'a'));
like image 156
a_horse_with_no_name Avatar answered Oct 15 '22 08:10

a_horse_with_no_name


9.5+

There is an easier method now

SELECT
  sArray,
  c,
  coalesce(array_length( array_positions(sArray, c), 1 ),0) AS count
FROM ( VALUES
  (ARRAY['a','b','c','a','a'], 'a'),
  (ARRAY['a','b','c'], 'd')
) AS t(sArray,c);

   sarray    | c | count 
-------------+---+-------
 {a,b,c,a,a} | a |     3
 {a,b,c}     | d |     0
(2 rows)
like image 42
NO WAR WITH RUSSIA Avatar answered Oct 15 '22 09:10

NO WAR WITH RUSSIA


The occurrence of all elements in an array can be found with this query:

SELECT count(id), UNNEST(array) as element
FROM myTable 
GROUP BY element;

To count the occurrence of a specific element, for example 'c', add a WHERE clause:

SELECT count(id), UNNEST(array) as element
FROM myTable 
WHERE EXISTS (SELECT * FROM UNNEST(array) AS x WHERE x='c')
GROUP BY element;
like image 2
TER Avatar answered Oct 15 '22 09:10

TER