Re: [GENERAL] SQL question re aggregates & joins

2010-01-28 Thread Tom Lane
Scott Ribe writes: > Given that t2.id is the primary key, grouping by any other column of t2 is > really redundant. I know *what* SQL won't allow me to do, I'm interested in > knowing if there's some reason *why* other than historical... SQL92 says so. More recent versions of the SQL spec descri

Re: [GENERAL] SQL question re aggregates & joins

2010-01-28 Thread Dann Corbit
> -Original Message- > From: pgsql-general-ow...@postgresql.org [mailto:pgsql-general- > ow...@postgresql.org] On Behalf Of Scott Ribe > Sent: Thursday, January 28, 2010 2:10 PM > To: Thom Brown > Cc: pgsql-general > Subject: Re: [GENERAL] SQL question re aggregat

Re: [GENERAL] SQL question re aggregates & joins

2010-01-28 Thread Scott Ribe
> You can't include an aggregate in the select if you don't group by > non-aggregates, so it should be: > > 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", t2."DateOfBirth"; I was aware that I could alter

Re: [GENERAL] SQL question re aggregates & joins

2010-01-28 Thread Thom Brown
On 28 January 2010 21:32, Scott Ribe wrote: > 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") >

[GENERAL] SQL question re aggregates & joins

2010-01-28 Thread Scott Ribe
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 gro