Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 21D1A9FA1FE for ; Fri, 1 Dec 2006 10:45:12 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 11662-09 for ; Fri, 1 Dec 2006 10:44:57 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.177]) by postgresql.org (Postfix) with ESMTP id B044F9FA178 for ; Fri, 1 Dec 2006 10:45:02 -0400 (AST) Received: from [194.25.154.138] (helo=webserv.wug.de) by mrelayeu.kundenserver.de (node=mrelayeu1) with ESMTP (Nemesis), id 0MKwpI-1Gq9dL1r7X-0000rm; Fri, 01 Dec 2006 15:45:00 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1Gq9dK-0003gA-00 for pgsql-sql@postgresql.org; Fri, 01 Dec 2006 15:44:58 +0100 Date: Fri, 1 Dec 2006 15:44:58 +0100 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: Setting boolean column based on cumulative integer value Message-ID: <20061201144458.GA13639@a-kretschmer.de> References: <9d8bdcf30612010550o3a5721lefc366e4984fd145@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9d8bdcf30612010550o3a5721lefc366e4984fd145@mail.gmail.com> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! User-Agent: Mutt/1.5.9i X-Provags-ID: kundenserver.de abuse@kundenserver.de login:51ea9561fc3f6d9539a2acd743e4748c X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/6 X-Sequence-Number: 26962 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