Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 4E5E6476070 for ; Fri, 29 Nov 2002 15:10:00 -0500 (EST) Received: from calvin.slamb.org (calvin.slamb.org [216.136.66.56]) by postgresql.org (Postfix) with ESMTP id D29CC475ECE for ; Fri, 29 Nov 2002 15:09:59 -0500 (EST) Received: from slamb.org (12-217-247-102.client.mchsi.com [12.217.247.102]) (using TLSv1 with cipher RC4-MD5 (128/128 bits)) (No client certificate requested) by calvin.slamb.org (Postfix) with ESMTP id 6913D6FC96 for ; Fri, 29 Nov 2002 14:11:44 -0600 (CST) Message-ID: <3DE7C916.5090409@slamb.org> Date: Fri, 29 Nov 2002 14:07:50 -0600 From: Scott Lamb User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.1) Gecko/20020826 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-general@postgresql.org Subject: Re: SQL Query References: <1038597838.1488.23.camel@localhost.localdomain> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/1455 X-Sequence-Number: 33874 Scott Taylor wrote: > I have submitted this query to the list before, but have since upgraded > to a later version so I lost the command history. Ergh. Command history? You really should stick your queries somewhere more permanent than that, even if it is one you only use in the query monitor. > From the below output, could someone tell me how to return rows only > where: where all of these are true? where any of these are true? > 1. If chart_id=10074, return all rows with same trans_id (i.e. trans_id > 10088 and 10101) Return all rows where there exists a row with the same trans_id and chart_id = 10074. (That's easy to express with an "exists" where clause.) Does the chart_id = 10074 row have to satisfy the two conditions below for rows with the same trans_id to be returned? Or just the rows being actually returned? > 2. Where amount >=0 > 3. With transdate between 2002-07-01 and 2002-09-30 My best guess is that you are looking for this: select trans_id, chart_id, amount, transdate from acc_trans where exists ( select 'x' from acc_trans sub where acc_trans.trans_id = sub.trans_id and sub.chart_id = 10074) and amount >= 0 and transdate between '2002-07-01' and '2002-09-30'