pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chad Miller <cmiller+ps@surfsouth.com>
To: pgsql-sql@postgresql.org
Subject: err: select f() from i where (f()) in (select f() from x group by j);
Date: Fri, 13 Aug 1999 20:13:00 -0400
Message-ID: <19990813201300.B11645@surfsouth.com> (raw)


Hi there.  Please forgive my igmorance.  

I have a table populated with lots of numbers, names, and timestamps.  I'd 
like to find the max of the sums of the numbers and the timestamp.

That is, find the timestamp and the sum of the numbers when the sum of 
the numbers is max.  Yow!

This does half -- the summing:

> select timestamp, sum(num) from timelines where (name='foo' or name='bar' or name='baz') group by timestamp;

If I create a view, with

> create view foo as select timestamp, sum(num) from timelines where (name='foo' or name='bar' or name='baz') group by timestamp;

I get: 
Table    = foo
+----------------------------------+----------------------------------+-------+
|              Field               |              Type                | Length|
+----------------------------------+----------------------------------+-------+
| timestamp                        | datetime                         |     8 |
| sum                              | int2                             |     2 |
+----------------------------------+----------------------------------+-------+

(Note the fields' naming)  Still -- I'd like a more elegant way to do this,
than creating a view.

I came up with:

> select timestamp, max(sum) from timelines where (timestamp, sum) in (select timestamp, sum(num) from timelines where (name='foo' or name='bar' or name='baz') group by timestamp);

...which returns 

< ERROR:  attribute 'sum' not found

The corresponding column in the view was titled ``sum.''  Hmmm.  So, perhaps 
fully writing the ``sum(num)'' is correct. 

> select timestamp from timelines where (timestamp, sum(num)) in (select timestamp, sum(num) from timelines where (name='foo' or name='bar' or name='baz') group by timestamp);

...which returns

< ERROR:  parser: illegal use of aggregates or non-group column in target list

Or...

> select timestamp, max(sum(num)) from timelines where (timestamp, sum(num)) in (select timestamp, sum(num) from timelines where (name='foo' or name='bar' or name='baz') group by timestamp);

...which returns

< ERROR:  type id lookup of 223198728 failed

This is making me itch.  Advice?  Wizardry?  Comments?  Flames?  

						- chad




view thread (2+ messages)  latest in thread

Message-ID: <19990813201300.B11645@surfsouth.com>
Permalink:  ../19990813201300.B11645@surfsouth.com/
Also on:    postgresql.org/message-id/19990813201300.B11645@surfsouth.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-sql@postgresql.org
  Cc: cmiller+ps@surfsouth.com
  Subject: Re: err: select f() from i where (f()) in (select f() from x group by j);
  In-Reply-To: <19990813201300.B11645@surfsouth.com>
  References: <19990813201300.B11645@surfsouth.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