Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (8.11.3/8.11.4) with ESMTP id g0OHwql13951 for ; Thu, 24 Jan 2002 12:58:52 -0500 (EST) (envelope-from josh@agliodbs.com) Received: from [209.10.40.250] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 3.5.2) with HTTP id 675145; Thu, 24 Jan 2002 09:58:56 -0800 From: "Josh Berkus" Subject: Re: why can a named subselect not be used in a where To: Markus Bertheau , postgres sql list X-Mailer: CommuniGate Pro Web Mailer v.3.5.2 Date: Thu, 24 Jan 2002 09:58:56 -0800 Message-ID: In-Reply-To: <1011894031.29186.24.camel@entwicklung01.cenes.de> MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Archive-Number: 200201/273 X-Sequence-Number: 6357 Markus, > cenes_test=> select personen.*, (select max(last_visit) from sessions > where sessions.personen_id = personen.personen_id) as max from > personen > where max between 1009148400 and 1011913200 order by max desc; > ERROR: Attribute 'max' not found > > I again think that the table structure is not neccesary to understand > my > question. > > Well, why can't I use max in the where clause? There's two problems with the above query that have nothing to do with named subselects, which are well-supported by PostgreSQL (in fact, as of 7.1.3, Postgres has better support for subselects of all sorts than any other major RDBMS platform). 1. "Max" is a reserved word in SQL. You are confusing the parser. Pick another name. 2. While you are already aware that the example query is not the most efficient construction, I also think you are doing your aliasing in the wrong place: select personen.*, (select max(last_visit) as max_visit from sessions where sessions.personen_id = personen.personen_id) from personen ... but I could be wrong, as I almost never find any reason to use a sub-select in the SELECT line. -Josh ______AGLIO DATABASE SOLUTIONS___________________________ Josh Berkus Complete information technology josh@agliodbs.com and data management solutions (415) 565-7293 for law firms, small businesses fax 621-2533 and non-profit organizations. San Francisco