Re: Selecting pairs of numbers

From: Igor Neyman <ineyman(at)perceptron(dot)com>
To: "rod(at)iol(dot)ie" <rod(at)iol(dot)ie>, 'PostgreSQL' <pgsql-general(at)postgresql(dot)org>
Subject: Re: Selecting pairs of numbers
Date: 2015-10-05 18:48:07
Message-ID: A76B25F2823E954C9E45E32FA49D70ECCD54DCBE@mail.corp.perceptron.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

-----Original Message-----
From: pgsql-general-owner(at)postgresql(dot)org [mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Raymond O'Donnell
Sent: Monday, October 05, 2015 2:40 PM
To: 'PostgreSQL' <pgsql-general(at)postgresql(dot)org>
Subject: [GENERAL] Selecting pairs of numbers

Hello all,

I have an SQL problem which ought to be simple, but I can't get my head around it.

I have pairs of integers - let's call them (x, y). In effect, x is a category, while y is an item within that category. For every x, there is always the same number of integers y; and both x and y are always numbered sequentially starting from 1.

My problem is that I need to select a list of these pairs, ordered first on x and then on y, from a given starting point to a given finishing point and including all pairs in between.

For example, I might have:

x | y
-----
1 | 1
1 | 2
1 | 3
1 | 4
2 | 1
2 | 2
2 | 3
2 | 4
(etc)

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. Any help will be appreciated, or even just a pointer in the right direction. There's probably something simple that I'm just not seeing....

If anyone's interested, these numbers represent channels and pistons on the combination system of a largish pipe organ... it's for a hobby project.

Many thanks in advance!

Ray.

____________________________________

SELECT x, y FROM my_table
WHERE (x*10 + y) >= (1*10 + 3) AND (x*10 + y) <= (3*10 + 2)
ORDER BY x, y;

Regards,
Igor Neyman

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Chris Mair 2015-10-05 18:50:11 Re: Selecting pairs of numbers
Previous Message Karsten Hilbert 2015-10-05 18:44:12 Re: md5(large_object_id)