今天帮同学看了一个问题,属于细节问题,小记一下。
问题:
有个sql,放pl/sql developer里面执行没有问题,但是用myeclipse启动服务器,java代码用jdbcTemplate执行查询,就报ORA-00979: not a GROUP BY expression
的错误。
select
trunc(tr.test_time, ?) time, round(avg(tr.latency), 0) latency
from
zy_test_task_desc tk, Test_Result tr
where
tk.agent_city = ?
and tk.test_type = ?
and tk.urlip = ?
and tr.test_time >= to_date(?, 'yyyy-MM-dd hh24:mi:ss')
and tr.test_time <= to_date(?, 'yyyy-MM-dd hh24:mi:ss')
and tk.task_id in
(
select
ct.task_id
from
user_auth t, customer_user cu, customer_task ct
where
t.password = ?
and t.user_name = ?
and t.user_id = cu.user_id
and cu.customer_id = ct.customer_id
)
and tk.task_id = tr.task_id
GROUP BY trunc(tr.test_time, ?)
参数
DD,nj,1,219.141.104.14,2011/9/1 00:00:00,2011/9/30 23:59:59,123456,zzb1,DD
看了半天没找到原因,后来还是同学找到根源所在。 问题出在trunc(tr.test_time, ?)上,如果不用动态参数,直接写成trunc(tr.test_time,'DD') mytime和GROUP BY trunc(tr.test_time,'DD')问题消失。这是为什么呢?
原来,动态传递即使传递相同的参数,它也认为是不同的,导致认为查询字段和group by的字段不同故报错。