From: A. Kretschmer <andreas.kretschmer@schollglas.com>
To: pgsql-sql@postgresql.org
Subject: Re: Setting boolean column based on cumulative integer value
Date: Fri, 1 Dec 2006 15:44:58 +0100
Message-ID: <20061201144458.GA13639@a-kretschmer.de> (raw)
In-Reply-To: <9d8bdcf30612010550o3a5721lefc366e4984fd145@mail.gmail.com>
References: <9d8bdcf30612010550o3a5721lefc366e4984fd145@mail.gmail.com>
am Sat, dem 02.12.2006, um 0:50:37 +1100 mailte Markus Juenemann folgendes:
> I've got a bit of a tricky (for me!) problem. The example below is
> completely ficticious but
> describes my real problem in a way which might be easier to understand.
>
> Imagine the table contains a list of passenger wanting to get on a
> small(!) plane.
> The plane can carry at most 200kg of passengers and will be filled
> strictly on a first-come
> first-serve basis - well, check-in staff is a bit stupid ;-). So what
> needs to be done is to set the 'gets_seat' column to true until the
> weight limit is reached.
With your example, i wrote a little function for this:
---%<------
create or replace function check_wight( out id int,
out name text,
out weight int,
out gets_seat boolean ) returns setof record as $$
declare rec record;
sum int;
begin
sum = 0;
for rec in select * from passenger_queue order by id LOOP
id = rec.id;
name = rec.name;
weight = rec.weight;
sum = sum + weight;
if sum < 200 then
gets_seat='t'::bool;
else
gets_seat='f'::bool;
end if;
return next ;
end loop;
end
$$ language plpgsql;
---%<------
test=# select * from passenger_queue;
id | name | weight | gets_seat
----+-------+--------+-----------
1 | Peter | 75 | f
2 | Mary | 50 | f
3 | John | 70 | f
4 | Steve | 80 | f
(4 rows)
test=# select * from check_wight();
id | name | weight | gets_seat
----+-------+--------+-----------
1 | Peter | 75 | t
2 | Mary | 50 | t
3 | John | 70 | t
4 | Steve | 80 | f
(4 rows)
HTH, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47215, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net
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: andreas.kretschmer@schollglas.com
Subject: Re: Setting boolean column based on cumulative integer value
In-Reply-To: <20061201144458.GA13639@a-kretschmer.de>
* 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