Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id C86ED9FB7DB for ; Wed, 25 Jul 2007 06:54:59 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 21091-10 for ; Wed, 25 Jul 2007 06:54:51 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from mu-out-0910.google.com (mu-out-0910.google.com [209.85.134.190]) by postgresql.org (Postfix) with ESMTP id 6AED29FB7C2 for ; Wed, 25 Jul 2007 06:54:56 -0300 (ADT) Received: by mu-out-0910.google.com with SMTP id i10so199732mue for ; Wed, 25 Jul 2007 02:54:54 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=VGElifsRuslusF7kXkrJVGqUQOYC0G5lK1+8yMKXE2SzzTf9R+lejN8Pg4RfY0+vlLGJO1L0JQQJttC09UWKKxDhRYeohbvo+FfoOdknUAMbZAMd81kJvkKGcJ5FNNROIA92NSwbOjUXHNNfVYBlj1pBwWBigPYXVircU5bEaXE= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=E0FDNZ9q0G3waPCaSJiLhbE1SNCOR1wllXFLth2/EWkPahUZbKfj+hr6atExDmx6MBBU7VmoC+A6emOJCpiy3quujcx5hFtbES98wfy5qx+xL+fYv31wUAtx2OrEmtvesO5IZEqOWfk8I0xX1jgol5R3D7u3BjFrKHtkE4T8Rqk= Received: by 10.82.108.9 with SMTP id g9mr205339buc.1185357294142; Wed, 25 Jul 2007 02:54:54 -0700 (PDT) Received: from ?192.168.1.33? ( [82.13.29.166]) by mx.google.com with ESMTPS id f8sm678869nfh.2007.07.25.02.54.46 (version=SSLv3 cipher=RC4-MD5); Wed, 25 Jul 2007 02:54:51 -0700 (PDT) Message-ID: <46A71DE3.6000205@enterprisedb.com> Date: Wed, 25 Jul 2007 10:54:43 +0100 From: Heikki Linnakangas User-Agent: Icedove 1.5.0.10 (X11/20070329) MIME-Version: 1.0 To: Marcos Truchado CC: pgsql-jdbc@postgresql.org Subject: Re: problem with date_trunc and jdbc References: <46A6891E.7090809@ya.com> In-Reply-To: <46A6891E.7090809@ya.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/114 X-Sequence-Number: 17288 Marcos Truchado wrote: > Suppose I have a table mytable with a column mydate of type date. > > if I launch this SQL directly with psql: > > SELECT date_trunc('month', mydate)::date FROM mytable WHERE > mytable.mydate BETWEEN '2000-07-25 +02:00' AND '2004-07-25 +02:00' GROUP > BY date_trunc('month', mydate); > > I obtaint the months/years in where exists any record in this table with > a date stored in mydate that uses this month/year, all between 2 dates > > This sql works ok under psql. But if I try to execute it with jdbc: > > String sql = "SELECT date_trunc('month', mydate)::date FROM mytable > WHERE mytable.mydate BETWEEN ? AND ? GROUP BY date_trunc('month', mydate)"; > > PreparedStatement pstmt = conn.prepareStatement(sql, > ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); > > Calendar calendar = Calendar.getInstance(); > > //initial date, suppose f_ini has the correct date > calendar.setTime(f_ini); > pstmt.setDate(1, new java.sql.Date(calendar.getTimeInMillis())); > > //end date, suppose f_end has the correct date > calendar.setTime(f_end); > pstmt.setDate(2, new java.sql.Date(calendar.getTimeInMillis())); > > pstmt.executeQuery(); > > The code works ok, but when it reaches to executeQuery() statement it > fails with message "mytable.mydate must be used in group by clause or in > aggregate function" What exactly is that the error message you're getting? I couldn't find that phrase anywhere in the PostgreSQL or the JDBC driver source code. > I'm using postgresql 8.1.9 and jdbc driver 8.1 build 409 JDBC3 I tried to reproduce this but it works fine for me. Can you construct a complete self-contained test program? -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com