pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedCASE(?) to write in a different column based on a string pattern
5+ messages / 3 participants
[nested] [flat]
* CASE(?) to write in a different column based on a string pattern
@ 2019-11-13 16:24 Moreno Andreo <moreno.andreo@evolu-s.it>
2019-11-13 16:36 ` Re: CASE(?) to write in a different column based on a string pattern Geoff Winkless <pgsqladmin@geoff.dj>
0 siblings, 1 reply; 5+ messages in thread
From: Moreno Andreo @ 2019-11-13 16:24 UTC (permalink / raw)
To: pgsql-general
Hi,
I need to create a CASE (I think) statement to check for a string
pattern, and based on its value, write a substring in a different column
(alias).
I'm trying to create a COPY statement to port a table into antoher
database, which has a table with another format (that's why the aliases)
Let's write it in pseudoSQL:
given this
select pattern from tbl;
pattern
----------
foo1234
bar5678
baz9012
That's what I'm trying to achieve
select
pattern,
CASE when pattern like 'foo%' then ltrim(pattern, 'bar') as foo
when pattern like 'bar%' then ltrim(pattern, 'bar')
as bar
when pattern like 'baz%' then ltrim(pattern, 'baz')
as baz
END
from tbl;
|foo |bar |baz |
1234
5678
9012
(hoping text formatting is ok... 1234 should go in column foo, 568 in
bar and 9012 in baz)
Is it possible?
Thanks in advance
Moreno.-
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: CASE(?) to write in a different column based on a string pattern
2019-11-13 16:24 CASE(?) to write in a different column based on a string pattern Moreno Andreo <moreno.andreo@evolu-s.it>
@ 2019-11-13 16:36 ` Geoff Winkless <pgsqladmin@geoff.dj>
2019-11-13 16:48 ` Re: CASE(?) to write in a different column based on a string pattern Andrew Kerber <andrew.kerber@gmail.com>
2019-11-13 17:03 ` Re: CASE(?) to write in a different column based on a string pattern Moreno Andreo <moreno.andreo@evolu-s.it>
0 siblings, 2 replies; 5+ messages in thread
From: Geoff Winkless @ 2019-11-13 16:36 UTC (permalink / raw)
To: Moreno Andreo <moreno.andreo@evolu-s.it>; +Cc: pgsql-general
On Wed, 13 Nov 2019 at 16:24, Moreno Andreo <moreno.andreo@evolu-s.it> wrote:
> |foo |bar |baz |
> 1234
> 5678
> 9012
> (hoping text formatting is ok... 1234 should go in column foo, 568 in
> bar and 9012 in baz)
>
> Is it possible?
Simplest way in plain SQL would be individual case statements for each
column, I think.
SELECT pattern,
CASE WHEN pattern LIKE 'foo%' THEN SUBSTR(pattern, 4) ELSE '' END AS foo
CASE WHEN pattern LIKE 'bar%' THEN SUBSTR(pattern, 4) ELSE '' END AS bar
CASE WHEN pattern LIKE 'baz%' THEN SUBSTR(pattern, 4) ELSE '' END AS baz
FROM tbl;
Geoff
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: CASE(?) to write in a different column based on a string pattern
2019-11-13 16:24 CASE(?) to write in a different column based on a string pattern Moreno Andreo <moreno.andreo@evolu-s.it>
2019-11-13 16:36 ` Re: CASE(?) to write in a different column based on a string pattern Geoff Winkless <pgsqladmin@geoff.dj>
@ 2019-11-13 16:48 ` Andrew Kerber <andrew.kerber@gmail.com>
2019-11-13 17:00 ` Re: CASE(?) to write in a different column based on a string pattern Moreno Andreo <moreno.andreo@evolu-s.it>
1 sibling, 1 reply; 5+ messages in thread
From: Andrew Kerber @ 2019-11-13 16:48 UTC (permalink / raw)
To: Geoff Winkless <pgsqladmin@geoff.dj>; +Cc: Moreno Andreo <moreno.andreo@evolu-s.it>; pgsql-general
So what you are doing is transforming the table format from vertical to
horizontal. I think you will want to use a union to join the table to
itself along with the case statement to produce the output you are looking
for.
On Wed, Nov 13, 2019 at 10:37 AM Geoff Winkless <pgsqladmin@geoff.dj> wrote:
> On Wed, 13 Nov 2019 at 16:24, Moreno Andreo <moreno.andreo@evolu-s.it>
> wrote:
> > |foo |bar |baz |
> > 1234
> > 5678
> > 9012
> > (hoping text formatting is ok... 1234 should go in column foo, 568 in
> > bar and 9012 in baz)
> >
> > Is it possible?
>
> Simplest way in plain SQL would be individual case statements for each
> column, I think.
>
> SELECT pattern,
> CASE WHEN pattern LIKE 'foo%' THEN SUBSTR(pattern, 4) ELSE '' END AS foo
> CASE WHEN pattern LIKE 'bar%' THEN SUBSTR(pattern, 4) ELSE '' END AS bar
> CASE WHEN pattern LIKE 'baz%' THEN SUBSTR(pattern, 4) ELSE '' END AS baz
> FROM tbl;
>
> Geoff
>
>
>
--
Andrew W. Kerber
'If at first you dont succeed, dont take up skydiving.'
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: CASE(?) to write in a different column based on a string pattern
2019-11-13 16:24 CASE(?) to write in a different column based on a string pattern Moreno Andreo <moreno.andreo@evolu-s.it>
2019-11-13 16:36 ` Re: CASE(?) to write in a different column based on a string pattern Geoff Winkless <pgsqladmin@geoff.dj>
2019-11-13 16:48 ` Re: CASE(?) to write in a different column based on a string pattern Andrew Kerber <andrew.kerber@gmail.com>
@ 2019-11-13 17:00 ` Moreno Andreo <moreno.andreo@evolu-s.it>
0 siblings, 0 replies; 5+ messages in thread
From: Moreno Andreo @ 2019-11-13 17:00 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Il 13/11/19 17:48, Andrew Kerber ha scritto:
> So what you are doing is transforming the table format from vertical
> to horizontal. I think you will want to use a union to join the table
> to itself along with the case statement to produce the output you are
> looking for.
>
Not precisely, the string pattern is only part of a bigger table (30
columns in total), what I'm trying to achieve is what Geoff explained,
just split values in 3 different columns based on the string pattern
Thanks for your time
Moreno.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: CASE(?) to write in a different column based on a string pattern
2019-11-13 16:24 CASE(?) to write in a different column based on a string pattern Moreno Andreo <moreno.andreo@evolu-s.it>
2019-11-13 16:36 ` Re: CASE(?) to write in a different column based on a string pattern Geoff Winkless <pgsqladmin@geoff.dj>
@ 2019-11-13 17:03 ` Moreno Andreo <moreno.andreo@evolu-s.it>
1 sibling, 0 replies; 5+ messages in thread
From: Moreno Andreo @ 2019-11-13 17:03 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Il 13/11/19 17:36, Geoff Winkless ha scritto:
>
> Simplest way in plain SQL would be individual case statements for each
> column, I think.
>
> SELECT pattern,
> CASE WHEN pattern LIKE 'foo%' THEN SUBSTR(pattern, 4) ELSE '' END AS foo
> CASE WHEN pattern LIKE 'bar%' THEN SUBSTR(pattern, 4) ELSE '' END AS bar
> CASE WHEN pattern LIKE 'baz%' THEN SUBSTR(pattern, 4) ELSE '' END AS baz
> FROM tbl;
>
> Geoff
>
>
Geoff,
it worked perfectly!
Thanks a lot!
Moreno.-
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2019-11-13 17:03 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-11-13 16:24 CASE(?) to write in a different column based on a string pattern Moreno Andreo <moreno.andreo@evolu-s.it>
2019-11-13 16:36 ` Geoff Winkless <pgsqladmin@geoff.dj>
2019-11-13 16:48 ` Andrew Kerber <andrew.kerber@gmail.com>
2019-11-13 17:00 ` Moreno Andreo <moreno.andreo@evolu-s.it>
2019-11-13 17:03 ` Moreno Andreo <moreno.andreo@evolu-s.it>
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