Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 8E69DB5DBE2 for ; Tue, 5 Jul 2011 21:47:33 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 18088-09 for ; Wed, 6 Jul 2011 00:47:27 +0000 (UTC) X-Greylist: delayed 07:05:08.307143 by SQLgrey-1.7.6 Received: from dime159.dizinc.com (dime159.dizinc.com [66.7.216.77]) by mail.postgresql.org (Postfix) with ESMTP id 9E583B5DBD0 for ; Tue, 5 Jul 2011 21:47:27 -0300 (ADT) Received: from [200.32.230.122] (port=58774 helo=[192.168.100.55]) by dime159.dizinc.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1Qe9dX-0003Ra-LY for pgsql-sql@postgresql.org; Tue, 05 Jul 2011 13:42:16 -0400 Message-ID: <4E134CF2.1040304@turnkey.bz> Date: Tue, 05 Jul 2011 11:42:10 -0600 From: "M. D." User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110516 Lightning/1.0b2 Thunderbird/3.1.10 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: group by with sum and sum till max date Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - dime159.dizinc.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - turnkey.bz X-Source: X-Source-Args: X-Source-Dir: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201107/16 X-Sequence-Number: 35586 This is a little hard to explain, and I'm not sure if it's possible, but here goes. This is my query: select year, month, (select number from account where account.account_id = view_account_change.account_id) as number, (select name from account where account.account_id = view_account_change.account_id) as account, sum(amount) as amount from view_account_change where view_account_change.change_date >= '2010-01-01' group by year,month, number, account order by year,month, number, account I want to make an exception for the sum so that if the account number is less than 4000, I want a sum of all transactions until the last date of the group by. the query for that would be: Select sum(amount) from view_account_change where change_date > "max date in the group" Is this possible? Thanks, Mark