pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Craig Ringer <craig@postnewspapers.com.au>
To: novice <user.postgresql@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: count question
Date: Wed, 09 Apr 2008 10:11:39 +0800
Message-ID: <47FC25DB.2090803@postnewspapers.com.au> (raw)
In-Reply-To: <ddcb1c340804081851y245f4776g9de74b5715e3a3e@mail.gmail.com>
References: <ddcb1c340804081851y245f4776g9de74b5715e3a3e@mail.gmail.com>

novice wrote:

> Is it possible to write a query to produce:
> 
>  meter_id | no_of_bays |   bay_id
> ----------+------------+-----------
>      5397 |          2 |   5397-01
>      5397 |          2 |   5397-02
>      5409 |          3 |   5409-01
>      5409 |          3 |   5409-02
>      5409 |          3 |   5409-03

Sure. One way, not necessarily a particularly clever or efficient way,
is to do a join with:

generate_series(1, (select max(no_of_bays) from meter)) as i

and use a WHERE clause to select for `i <= no_of_bays'


eg:

SELECT
    meter_id, no_of_bays, meter_id::text||'-'||i::text AS bay_id
FROM meter,
     generate_series(1, (select max(no_of_bays) from meter)) as i
WHERE i <= no_of_bays;


--
Craig Ringer



view thread (9+ messages)  latest in thread

Message-ID: <47FC25DB.2090803@postnewspapers.com.au>
Permalink:  ../47FC25DB.2090803@postnewspapers.com.au/
Also on:    postgresql.org/message-id/47FC25DB.2090803@postnewspapers.com.au

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: craig@postnewspapers.com.au, user.postgresql@gmail.com
  Subject: Re: count question
  In-Reply-To: <47FC25DB.2090803@postnewspapers.com.au>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox