我有一个如下查询,其中包含已实现的行号
select @running:=if(@previous= concat(inouts.days,inouts.element_id,
inouts.slot_id),@running,0) + 1 as rownum,
@previous:=concat(inouts.days,inouts.element_id,inouts.slot_id),
element_value,element_id,date_time,slot_id,days
from in_out_element_entry inouts where id_number='1234'
and element_id>12
order by days,element_id,slot_id,date_time
这将返回结果,如附图所示:
rownum | @previous....|element_value|element_id| date_time |time_slot |days |
-------------------------------------------------------------------------------------
1 | ... | 30 | 21 |2013-10-09 14:33:26 | 25 | 0
1 | ... | 30 | 21 |2013-10-09 14:33:26 | 25 | 0
2 | ... | 30 | 21 |2013-10-09 14:33:40 | 25 | 0
1 | ... | 130 | 22 |2013-10-09 14:33:26 | 25 | 0
2 | ... | 130 | 22 |2013-10-09 14:33:40 | 25 | 0
1 | ... | 900 | 23 |2013-10-09 14:33:26 | 26 | 0
2 | ... | 900 | 23 |2013-10-09 14:33:40 | 26 | 0
但是当我尝试使用 JDBC 运行此查询时,所有行号都是 1 。
我什至尝试通过存储过程执行相同的查询。并从 Java Dao 类中调用 SP。但最终结果会发生变化。所有的 rownumber 列都变为 1。
我不知道该怎么做或为什么会发生。有人可以帮忙吗?
我创建一个这样的存储过程:
create or replace procedure sp_in_out_report(
IN p_id_number VARCHAR(45))
begin
start transaction ;
insert into r_in_out(id_number,days,total_in)
select p_ip_number,days,sum(element_value) as total_in from
(select @running:=if(@previous= concat(inouts.days,inouts.element_id,
inouts.slot_id),@running,0) + 1 as rownum,
@previous:=concat(inouts.days,inouts.element_id,inouts.slot_id),
element_value,element_id,date_time,slot_id,days
from in_out_element_entry inouts where id_number=p_id_number
and element_id>12
order by days,element_id,slot_id,date_time )where rownum=1 group by days
commit;
end
然后从 Java 调用,如图所示:
CallableStatement callableStatement = null;
String procedureCall = "{call sp_ins_out_summary_report(?)}";
callableStatement = connection.prepareCall(procedureCall);
callableStatement.setString("p_id_number", idNum);
callableStatement.executeUpdate();
基本上我需要行号才能根据 date_time 从每个插槽中获取任何 element_id 的最新值
谢谢