I have a table item name which is like this :
Microsoft Word
Adobe Premiere
Paint
Mozila Firefox
Adobe Photoshop CS7
Windows Movie Maker
I want to select the data (table product, column name) become like this :
Microsoft
Word
Microsoft Word
Adobe
PremiereF
Adobe Premier
Paint
Mozila firefox
Adobe
Photoshop
CS7
Adobe Photoshop
Photoshop CS7
Windows
Movie
Maker
I’m using Postgres…. is it possible to make like that?
2
Answers
You could use
regexp_split_to_array
:Rextester Demo
EDIT:
To get every combination you could use:
Rextester Demo 2
db<>fiddle
It is not really clear to me what’s your expected result.
For
Adobe Photoshop CS7
your results are:What about the origin string
Adobe Photoshop CS7
? For the solution I am expecting you are wanting all subphrase in the right order. So the solution should include theAdobe Photoshop CS7
result. This is indicated by your other results which include the origin strings.(1) First step: get all subphrases from beginning:
Query
A: The
WITH
query makes the query simpler for writing the same subquery only one time (used in (2)). Theregexp_split_to_table
function splits the string at whitespaces and puts each word into a single line.B: The window function
row_number
adds a counter to the words which indicates the origin position within the original string (https://www.postgresql.org/docs/current/static/tutorial-window.html).C: The window function
array_agg() OVER (... ORDER BY nth_word)
aggregates the words into a list. TheORDER BY
is used to get an ascending word list indicated by the original word position (without theORDER BY
thearray_agg
would add all words of the phrase getting the original string for allword
rows)(2) Second step: get all subphrases from all starting points:
Query
A: Same as in (1)
B: cross join the phrases with themself; better speaking: join on word with each following of the same phrase
C: this window function aggregates the phrase words to the given result.
If you don’t like the array you could convert the result into a string with the function
array_to_string(phrase_part, ' ')