pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joel Burton <joel@joelburton.com>
To: Scott Taylor <scott.taylor@4i-dotcom.com>
Cc: pgsql-general@postgresql.org
Subject: Re: SQL Query
Date: Sat, 30 Nov 2002 02:45:44 -0500
Message-ID: <20021130074544.GA17708@temp.joelburton.com> (raw)
In-Reply-To: <1038597838.1488.23.camel@localhost.localdomain>
References: <1038597838.1488.23.camel@localhost.localdomain>

On Fri, Nov 29, 2002 at 07:23:56PM +0000, 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.
> 
> >From the below output, could someone tell me how to return rows only
> where:
> 
> 1. If chart_id=10074, return all rows with same trans_id (i.e. trans_id
> 10088 and 10101)
> 2. Where amount >=0
> 3. With transdate between 2002-07-01 and 2002-09-30
> 
> accs=# select trans_id, chart_id, amount, transdate from acc_trans;
>  trans_id | chart_id |  amount  | transdate
> ----------+----------+----------+------------
>     10088 |    10004 | -2062.12 | 2002-01-03
>     10088 |    10037 |     1755 | 2002-01-03
>     10088 |    10074 |   307.12 | 2002-01-03
>     10088 |    10004 |  2062.12 | 2002-07-03
>     10088 |    10002 | -2062.12 | 2002-07-03
>     10096 |    10016 |  1169.75 | 2002-12-03
>     10096 |    10047 | -1169.75 | 2002-12-03
>     10096 |    10002 |  1169.75 | 2002-11-03
>     10096 |    10016 | -1169.75 | 2002-11-03
>     10098 |    10016 |   283.91 | 2002-12-03
>     10098 |    10044 |  -283.91 | 2002-12-03
>     10099 |    10016 |    137.6 | 2002-12-03
>     10099 |    10045 |   -137.6 | 2002-12-03
>     10100 |    10016 |   163.74 | 2002-12-03
>     10100 |    10046 |  -163.74 | 2002-12-03
>     10101 |    10004 |  -528.75 | 2002-03-20
>     10101 |    10037 |      450 | 2002-03-20
>     10101 |    10074 |    78.75 | 2002-03-20

It'd be helpful if you gave us the solution you expect for this sample
data, BTW.

Interpreting your question, I get:

  0) Find all trans_id #s where chart_id=10074
  1) Find all rows w/those trans_id where
      a) the amount >=0
      b) the date is between 7/1 and 9/30

so only the fourth record would be returned.


So something like:

SELECT * 
FROM trans
WHERE
  trans_id IN (SELECT trans_id
               FROM   trans
	       WHERE  chart_id = 10074 )
  AND amount >= 0
  AND transdate BETWEEN '2002-07-01' AND '2002-09-30'

would be the easiest-to-understand solution, but it won't perform
terribly well (because of the IN statement). You can rewrite this
w/EXISTS or with a multi-table join, and it should perform better:

SELECT t2.*
FROM trans AS t1,
  trans AS t2
WHERE
  t1.chart_id = 10074
  AND t1.trans_id = t2.trans_id
  AND t2.amount >= 0
  AND t2.transdate BETWEEN '2002-07-01' AND '2002-09-30'

but you should test w/your data and indexes to check performance.


-- 

Joel BURTON  |  joel@joelburton.com  |  joelburton.com  |  aim: wjoelburton
Independent Knowledge Management Consultant



view thread (45+ messages)  latest in thread

Message-ID: <20021130074544.GA17708@temp.joelburton.com>
Permalink:  ../20021130074544.GA17708@temp.joelburton.com/
Also on:    postgresql.org/message-id/20021130074544.GA17708@temp.joelburton.com

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: joel@joelburton.com, scott.taylor@4i-dotcom.com
  Subject: Re: SQL Query
  In-Reply-To: <20021130074544.GA17708@temp.joelburton.com>
  References: <1038597838.1488.23.camel@localhost.localdomain> <20021130074544.GA17708@temp.joelburton.com>

* 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