Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

How would I do this in postgres :

Lets say I have an x and a y and I want to show all possible combinations :

xx
yy
xy
yx

How can I make postgres do this for me ?

share|improve this question

1 Answer 1

up vote 7 down vote accepted
with characters (c) as (
  select unnest(string_to_array('xy', null))
) 
select *
from characters c1
  cross join characters c2

Edit:

Apparently this does not work in 8.4, but the following should:

with characters (c) as (
  select unnest(regexp_split_to_array('xy', ''))
) 
select *
from characters c1
  cross join characters c2

Thanks to Bruno for testing this.

share|improve this answer
    
that gives me no output I'm affraid :/ –  Lucas Kauffman Dec 11 '11 at 16:30
    
No output? Works fine over here, PostgreSQL version 9.1 used to test. –  Frank Heikens Dec 11 '11 at 16:32
    
I'm still running 8.4 :( –  Lucas Kauffman Dec 11 '11 at 16:37
1  
@LucasKauffman: That should work on 8.4 as well (it has CTEs, unnest and string_to_array) –  a_horse_with_no_name Dec 11 '11 at 16:53
2  
I couldn't get it to work with 8.4 either, but replacing string_to_array('xy', null) with regexp_split_to_array('xy', '') works. Credit to this thread, which may have something to do with support for NULL as a delimiter in more recent versions. –  Bruno Dec 11 '11 at 20:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.