Re: Selecting pairs of numbers - Mailing list pgsql-general

From Adrian Klaver
Subject Re: Selecting pairs of numbers
Date
Msg-id 5612C731.405@aklaver.com
Whole thread Raw
In response to Selecting pairs of numbers  (Raymond O'Donnell <rod@iol.ie>)
Responses Re: Selecting pairs of numbers  (Raymond O'Donnell <rod@iol.ie>)
Re: Selecting pairs of numbers  ("Charles Clavadetscher" <clavadetscher@swisspug.org>)
List pgsql-general
On 10/05/2015 11:39 AM, Raymond O'Donnell wrote:
> 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....

aklaver@test=> create table pr_test(x int, y int);

aklaver@test=> select * from pr_test  where (x, y) between (1, 3) and
(3,2) order by x,y;
  x | y
---+---
  1 | 3
  1 | 4
  2 | 1
  2 | 2
  2 | 3
  2 | 4
  3 | 1
  3 | 2

>
> 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.
>
>


--
Adrian Klaver
adrian.klaver@aklaver.com


pgsql-general by date:

Previous
From: Chris Mair
Date:
Subject: Re: Selecting pairs of numbers
Next
From: Igor Neyman
Date:
Subject: Re: Selecting pairs of numbers