pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Chuck Martin <clmartin@theombudsman.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: HAVING query structured wrong
Date: Mon, 18 Feb 2019 17:37:10 +0000
Message-ID: <87va1hhun8.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAFw6=U1HjkB-V5SWmi60m6LvDJpfSg26rLD37dPAdKfGQKOfNw@mail.gmail.com>
References: <CAFw6=U1HjkB-V5SWmi60m6LvDJpfSg26rLD37dPAdKfGQKOfNw@mail.gmail.com>

>>>>> "Chuck" == Chuck Martin <clmartin@theombudsman.com> writes:

 Chuck> I am trying to create a query that returns all transactions for
 Chuck> each person who has a balance over a given amount. I thought
 Chuck> HAVING was the answer, but if so, I'm mis-using it. This query
 Chuck> returns only transactions exceeding the given amount rather than
 Chuck> transactions for people whose balance is over the amount:
 [snip]
 Chuck> Since that returned the wrong set of records, I created another
 Chuck> that returns the correct set of people with balances over the
 Chuck> given amount. But I can't figure out how to use this to get all
 Chuck> the transactions for people returned by this query:

 Chuck> SELECT case_pkey
 Chuck> FROM trans,ombcase,status
 Chuck> WHERE case_fkey = case_pkey
 Chuck> AND status_fkey = status_pkey
 Chuck> AND statusopen = 1
 Chuck> AND transistrust <> 1
 Chuck> AND transcleared <> 1
 Chuck> GROUP BY case_pkey
 Chuck> HAVING sum(transamount) >= 50

 Chuck> ORDER BY case_pkey

 Chuck> So how do I get all transactions for each case_pkey?

You can join the result of any subquery as if it were a table, either
with or without using a CTE:

SELECT ...
  FROM (select case_pkey from ... having ...) AS cases,
       trans
 WHERE trans.case_fkey = cases.case_pkey;

(incidentally, please qualify all the column references in your query
with a table name or alias, otherwise people reading your code have no
idea which column is supposed to be in which table)

or with a CTE,

WITH cases AS (select ... from ... having ...)
SELECT ...
  FROM cases, trans
 WHERE trans.case_fkey = cases.case_pkey;

There's also a third method with window functions instead of GROUP BY,
which is to do something like

SELECT ...
  FROM (select ...,
               sum(transamount) over (partition by case_pkey) as total_amt
          from ...) s
 WHERE total_amt > 50;

-- 
Andrew (irc:RhodiumToad)




view thread (3+ messages)  latest in thread

Message-ID: <87va1hhun8.fsf@news-spur.riddles.org.uk>
Permalink:  ../87va1hhun8.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87va1hhun8.fsf@news-spur.riddles.org.uk

 · 

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: andrew@tao11.riddles.org.uk, clmartin@theombudsman.com
  Subject: Re: HAVING query structured wrong
  In-Reply-To: <87va1hhun8.fsf@news-spur.riddles.org.uk>
  References: <CAFw6=U1HjkB-V5SWmi60m6LvDJpfSg26rLD37dPAdKfGQKOfNw@mail.gmail.com> <87va1hhun8.fsf@news-spur.riddles.org.uk>

* 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