日期:2014-05-17 浏览次数:20922 次
--此处假设status为0是失败,1成功 select companyid,max(createtime) d from 你的表 group by companyid having (count(1)>1 and count(decode(status,0,null,1))=0) or (count(1)=1);
------解决方案--------------------
记得在companyid列建立索引
------解决方案--------------------
是要一次性,所有公司都查出来,还是一个公司 一个公司的查?
如果是所有公司都查出来,建议走后台 存储过程。
把所有逻辑写到后台PRC里,计算出来后,前台JAVA只管查结果表就好了。
------解决方案--------------------
建表语句,数据准备,能不能发出来?
老是回答者建表,搞数据,麻烦啊。
。。。。。。
------解决方案--------------------
create or replace procedure proc_history h_id in number,--输入某公司的ID is h_countid integer; h_countstate integer; begin select count(*) into h_countid from Table_history where id=h_id; if h_countid=1 then select * from Table_history where status=0 and id=h_id;--显示状态为失败(0)的数据 end if; select count(*) into h_countstate from Table_history where status=1; if h_countstate=0 then select * from (select * from Table_history order by createtime desc) where rownum=1 end if; end;
------解决方案--------------------
-- 0:success 1:failed WITH tb AS ( SELECT 1 tid,'c1' copmanyid,0 status,TO_DATE('2011-04-01 15:10:42','yyyy-mm-dd hh24:mi:ss') createtime FROM DUAL UNION ALL SELECT 2 tid,'c2' copmanyid,1 status,TO_DATE('2011-04-02 15:10:42','yyyy-mm-dd hh24:mi:ss') createtime FROM DUAL UNION ALL SELECT 3 tid,'c3' copmanyid,1 status,TO_DATE('2011-04-03 15:10:42','yyyy-mm-dd hh24:mi:ss') createtime FROM DUAL UNION ALL SELECT 4 tid,'c3' copmanyid,0 status,TO_DATE('2011-04-04 15:10:42','yyyy-mm-dd hh24:mi:ss') createtime FROM DUAL UNION ALL SELECT 5 tid,'c3' copmanyid,1 status,TO_DATE('2011-04-05 15:10:42','yyyy-mm-dd hh24:mi:ss') createtime FROM DUAL UNION ALL SELECT 6 tid,'c4' copmanyid,1 status,TO_DATE('2011-04-08 15:10:42','yyyy-mm-dd hh24:mi:ss') createtime FROM DUAL UNION ALL SELECT 7 tid,'c4' copmanyid,1 status,TO_DATE('2011-04-07 15:10:42','yyyy-mm-dd hh24:mi:ss') createtime FROM DUAL UNION ALL SELECT 8 tid,'c4' copmanyid,1 status,TO_DATE('2011-04-09 15:10:42','yyyy-mm-dd hh24:mi:ss') createtime FROM DUAL ) SELECT n.tid, n.copmanyid, n.status, n.createtime FROM (SELECT m.tid, m.copmanyid, m.status, m.createtime, ROW_NUMBER() OVER(PARTITION BY m.copmanyid ORDER BY m.createtime DESC, m.tid ASC) rn FROM (SELECT t.*, COUNT(*) OVER(PARTITION BY t.copmanyid) total_cnt, SUM(DECODE(t.status, 0, 1, 0)) OVER(PARTITION BY t.copmanyid) success_total_cnt FROM tb t) m WHERE (m.total_cnt = 1 AND m.status = 1) OR (m.total_cnt > 1 AND m.success_total_cnt = 0)) n WHERE n.rn <= 2; TID COPMANYID STATUS CREATETIME ---------- --------- ---------- ----------- 2 c2 1 2011/04/02 8 c4 1 2011/04/09 6 c4 1 2011/04/08