pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Scott Lamb <slamb@slamb.org>
To: pgsql-general@postgresql.org
Subject: Re: SQL Query
Date: Fri, 29 Nov 2002 14:07:50 -0600
Message-ID: <3DE7C916.5090409@slamb.org> (raw)
References: <1038597838.1488.23.camel@localhost.localdomain>

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'




view thread (45+ messages)  latest in thread

Message-ID: <3DE7C916.5090409@slamb.org>
Permalink:  ../3DE7C916.5090409@slamb.org/
Also on:    postgresql.org/message-id/3DE7C916.5090409@slamb.org

 · 

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: slamb@slamb.org
  Subject: Re: SQL Query
  In-Reply-To: <3DE7C916.5090409@slamb.org>
  References: <1038597838.1488.23.camel@localhost.localdomain> <3DE7C916.5090409@slamb.org>

* 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