I have a COLUMN in a Postgres table which has values with the following pattern.
INPUT-X
INPUT-X-Y
INPUT-X-Y-Z
INPUT-X-Y-Z-A-...
I am trying to count the number of entries in COLUMN where the value begins with INPUT
and contains a given number of hyphens
. So if the input value is 2023-08-04T09:00:32.822+00:00
, I want to count all of the entries in COLUMN which begin with 2023-08-04T09:00:32.822+00:00
and contain 3
hyphens. If values in COLUMN include:
2023-08-04T09:00:32.822+00:00-1
2023-08-04T09:00:32.822+00:00-1-1
2023-08-04T09:00:32.822+00:00-2
2023-08-04T09:00:32.822+00:00-2-1
2023-08-04T09:00:32.822+00:00-3
2023-08-04T09:00:32.822+00:00-4
The count of this operation should equal 4
. (The purpose of this is to calculate the index for the next value of the series, which in this case would be 2023-08-04T09:00:32.822+00:00-5
. Each of the values are a reference to a parent entry e.g. 2023-08-04T09:00:32.822+00:00-5
signifies the 5th reply to entry 2023-08-04T09:00:32.822+00:00
. Likewise, 2023-08-04T09:00:32.822+00:00-2-3
signifies the 3rd reply to entry 2023-08-04T09:00:32.822+00:00-2
.)
The code I'm currently using is:
select count(*) from messages_dev where ref like input || '%-' || repeat('-', hyphens) || '%' into result;
but that is returning 0, for counts that should have higher numbers.
Can anybody recommend a way to get this count in Postgres?