From: Bricklen Anderson <banderson@presinet.com>
To: Mauro Bertoli <bertolima@yahoo.it>
Cc: pgsql-sql@postgresql.org
Subject: Re: Get max value from an comma separated string
Date: Mon, 05 Jun 2006 07:49:02 -0700
Message-ID: <4484445E.2000006@presinet.com> (raw)
In-Reply-To: <20060529112238.61079.qmail@web27009.mail.ukl.yahoo.com>
References: <20060529112238.61079.qmail@web27009.mail.ukl.yahoo.com>
Mauro Bertoli wrote:
> Hi, I've a field that contain values-comma-separated
> like
> A) 1;2;3;;5 -- ;2;;4;5
> but also
> B) 12;34;18
> how I can get the max value?
> For A I tried:
> SELECT max(array_upper(string_to_array(answer,';'),1))
> FROM values;
> and work fine, but for B case I don't find a solution
> like
> SELECT max(string_to_array(answer,';')) FROM values;
>
> Any ideas?
> Thanks for any hint
>
You could try rearranging the values into rows, like so:
CREATE OR REPLACE FUNCTION text2rows (TEXT,TEXT) RETURNS SETOF TEXT AS $$
SELECT (string_to_array($1, $2))[x.i]
FROM generate_series(1,array_upper(string_to_array($1,$2),1)) AS x(i);
$$ language sql strict;
select max(val)
from (SELECT text2rows(answer,';') as val FROM answer) as t;
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: banderson@presinet.com, bertolima@yahoo.it
Subject: Re: Get max value from an comma separated string
In-Reply-To: <4484445E.2000006@presinet.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox