Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

329
Views
PostgreSQL - Finding the most common substring() in an array

I need to find a way to determine the most common substring from within an array in PostgreSQL.

I've got a single dimension array in a column in PostgreSQL that is storing CPV values (a nested classification vocabulary - https://simap.ted.europa.eu/cpv). The codes made up of numeric characters, but stored as varchar as some records have a leading zero, like this:

["45331110", "50721000", "45251250", "42160000", "39715000", "45315000", "09323000", "71321200", "45331100", "50720000"]

I want to extract the most common leading two digits from this array using PostgreSQL, which in the example case would be 45.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

If you want to get the most common leading two digits per row, then you can use:

WITH data_rows(id, cpv_values) AS (
    VALUES (1, ARRAY ['45331110', '50721000', '45251250','42160000','39715000','45315000', '09323000','71321200','45331100', '50720000'])
         , (2, ARRAY ['50721000']) -- second test case
)
SELECT id, leading_two_digits
FROM data_rows
-- for every row in `data_rows` (your table),
-- select the most common `leading_two_digits` (through GROUP BY/ORDER BY/LIMIT 1)
JOIN LATERAL (
    SELECT left(code, 2) AS leading_two_digits
    FROM unnest(cpv_values) AS f(code)
    GROUP BY left(code, 2)
    ORDER BY COUNT(*) DESC
    LIMIT 1
) s ON true

returns

+--+------------------+
|id|leading_two_digits|
+--+------------------+
|1 |45                |
|2 |50                |
+--+------------------+

If you want to get the most common leading two digits across all rows, you can use:

WITH data_rows(cpv_values) AS (
    VALUES (ARRAY ['45331110', '50721000', '45251250','42160000','39715000','45315000', '09323000','71321200','45331100', '50720000']),
           (ARRAY ['45'])
)
SELECT left(code, 2) AS leading_two_digits
FROM data_rows, unnest(cpv_values) AS f(code)
GROUP BY left(code, 2)
ORDER BY COUNT(*) DESC
LIMIT 1
over 4 years ago · Santiago Trujillo Report

0

This query does what you need.

select substr(t, 1, 2) mc
 from unnest(array['45331110', '50721000', '45251250', '42160000', '39715000', '45315000', '09323000', '71321200', '45331100', '50720000']) t 
 group by mc
 order by count(1) desc
 limit 1;

Result:

Name|Value|
----|-----|
mc  |45   |

You may use thie above as a subquery to extract the most common substring per row.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!