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 ---------------------------(end of broadcast)--------------------------- TIP 2: Don't 'kill -9' the postmaster