SQL question re aggregates & joins

From: Scott Ribe <scott_ribe(at)killerbytes(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: SQL question re aggregates & joins
Date: 2010-01-28 21:32:49
Message-ID: C7875091.CDFB1%scott_ribe@killerbytes.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

OK, this does not work:

select max(t1."When"), t1."Pt_Id", t2."DateOfBirth"
from "PtStaffAccess" t1, "Person" t2
where t1."Pt_Id" = t2.id
group by t1."Pt_Id";

But this does:

select max(t1."When"), t1."Pt_Id", min(t2."DateOfBirth")
from "PtStaffAccess" t1, "Person" t2
where t1."Pt_Id" = t2.id
group by t1."Pt_Id";

Now the error message was clear, and I think PG is following the standard
here. But I have a question just for my own education.

It seems to me, given that "Person".id is declared as the primary key, it
should possible to deduce that there is no way that the 1st query could ever
have multiple values of "DateOfBirth" to choose from when building a result
row. Am I missing something? Or am I right, that this is something that SQL
could do but simply doesn't, for whatever reason, historical, complexity...

In fact, what's even more surprising to me, is that if I change the grouping
to the other side of the join, it still doesn't work:

select max(t1."When"), t2.id, t2."DateOfBirth"
from "PtStaffAccess" t1, "Person" t2
where t1."Pt_Id" = t2.id
group by t2.id;

Come on, I'm grouping on the primary key and it thinks that there might be
multiple values for the other columns?

--
Scott Ribe
scott_ribe(at)killerbytes(dot)com
http://www.killerbytes.com/
(303) 722-0567 voice

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Hardwick, Joe 2010-01-28 21:42:34 SET statement_timeout problem
Previous Message Mike Bresnahan 2010-01-28 19:02:28 Re: Amazon EC2 CPU Utilization