Re: Selecting pairs of numbers

From: Chris Mair <chris(at)1006(dot)org>
To: rod(at)iol(dot)ie, 'PostgreSQL' <pgsql-general(at)postgresql(dot)org>
Subject: Re: Selecting pairs of numbers
Date: 2015-10-05 18:50:11
Message-ID: bb61f220a0ba4aa1c42740957772ab0b@smtp.hushmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> I then might want to extract a list from, say, (1, 3) to (3, 2), giving:
>
> x | y
> -----
> 1 | 3
> 1 | 4
> 2 | 1
> 2 | 2
> 2 | 3
> 2 | 4
> 3 | 1
> 3 | 2
>
> For the life of me, I can't figure out how to do this.

Hi,

starting from this:

chris=# select * from t order by x,y;
x | y
---+---
1 | 1
1 | 2
1 | 3
1 | 4
2 | 1
2 | 2
2 | 3
2 | 4
3 | 1
3 | 2
3 | 3
3 | 4
(12 rows)

one trick that might help is this:

chris=# select * from t where x*1000+y >= 1003 and x*1000+y <= 3002 order by x,y;
x | y
---+---
1 | 3
1 | 4
2 | 1
2 | 2
2 | 3
2 | 4
3 | 1
3 | 2
(8 rows)

watch out, if you have y values bigger than 1000, though...

Bye,
chris.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2015-10-05 18:53:37 Re: Selecting pairs of numbers
Previous Message Igor Neyman 2015-10-05 18:48:07 Re: Selecting pairs of numbers