pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedproblem with date_trunc and jdbc
2+ messages / 2 participants
[nested] [flat]
* problem with date_trunc and jdbc
@ 2007-07-24 23:19 Marcos Truchado <mtruchado@ya.com>
2007-07-25 09:54 ` Re: problem with date_trunc and jdbc Heikki Linnakangas <heikki@enterprisedb.com>
0 siblings, 1 reply; 2+ messages in thread
From: Marcos Truchado @ 2007-07-24 23:19 UTC (permalink / raw)
To: pgsql-jdbc
Hello people, I have an strange problem here.
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"
Where is the fault?.
I'm using postgresql 8.1.9 and jdbc driver 8.1 build 409 JDBC3
Best regards
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: problem with date_trunc and jdbc
2007-07-24 23:19 problem with date_trunc and jdbc Marcos Truchado <mtruchado@ya.com>
@ 2007-07-25 09:54 ` Heikki Linnakangas <heikki@enterprisedb.com>
0 siblings, 0 replies; 2+ messages in thread
From: Heikki Linnakangas @ 2007-07-25 09:54 UTC (permalink / raw)
To: Marcos Truchado <mtruchado@ya.com>; +Cc: pgsql-jdbc
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
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2007-07-25 09:54 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-07-24 23:19 problem with date_trunc and jdbc Marcos Truchado <mtruchado@ya.com>
2007-07-25 09:54 ` Heikki Linnakangas <heikki@enterprisedb.com>
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