12
返回列表 发新帖
楼主: nicole1108

请教:如何实现?

[复制链接]
论坛徽章:
60
蛋疼蛋
日期:2012-03-22 20:34:08马上有钱
日期:2014-10-29 08:09:52马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11凯迪拉克
日期:2013-11-12 19:29:26
11#
发表于 2001-12-12 15:59 | 只看该作者
SQL> create table table1
  2  (pay varchar2(50),
  3  ttime date);

Table created

insert....

SQL> select * From table1;

PAY                                                TTIME
-------------------------------------------------- -----------
a                                                  2001-12-12 15:49:10
a                                                  2001-12-12 15:49:11
a                                                  2001-12-12 15:49:11
a                                                  2001-12-12 15:49:12
a                                                  2001-12-12 15:49:12
b                                                  2001-12-12 15:49:24
c                                                  2001-12-12 15:49:27
d                                                  2001-12-12 15:49:30
d                                                  2001-12-12 15:49:32

create or replace procedure test12 is
type times is table of number index by binary_integer;
type pays is table of varchar2(50) index by binary_integer;

t_times times;
t_pay pays;

cursor cursor1 is
  select pay from table1
  order by pay;

v_oldpay table1.pay%type;
v_newpay table1.pay%type;
v_times number;
i integer;

begin
  v_times := 0;
  v_oldpay := 'test';
  i := 1;
  Open cursor1;
  loop
    fetch cursor1 into v_newpay;
    exit when cursor1%notfound;
    if v_oldpay = 'test' then
      v_oldpay := v_newpay;
      v_times := v_times + 1;
    elsif v_newpay = v_oldpay then
      v_times := v_times + 1;
    else
      t_times(i) := v_times;
      t_pay(i) := v_oldpay;
      dbms_output.put_line('pay:'||t_pay(i)||'---times:'||t_times(i));
      i := i + 1;
      v_times := 1;
      v_oldpay := v_newpay;
    end if;
  end loop;
  t_times(i) := v_times;
  t_pay(i) := v_oldpay;
  dbms_output.put_line('pay:'||t_pay(i)||'---times:'||t_times(i));

  close cursor1;

end test12;
/

SQL> set serveroutput on;
SQL> begin
  2  dbms_output.enable;
  3  end;
  4  /

PL/SQL 过程已成功完成。

SQL> exec test12;
pay:a---times:5
pay:b---times:1
pay:c---times:1
pay:d---times:2

PL/SQL 过程已成功完成。

SQL>

使用道具 举报

回复
论坛徽章:
25
ITPUB元老
日期:2005-02-28 12:57:00管理团队成员
日期:2011-05-07 01:45:082012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14
12#
发表于 2001-12-12 16:09 | 只看该作者

首先不考虑效率问题

用PLSQL+Temporary table可以实现, 简单的循环计数而已, 有什么特殊要求么?

因为是海量数据, 需要考虑性能的地方是:
1. 首先对该表排序, 需要占用大量的sort_area和临时段, 如果这样的操作是经常的, 可以考虑在time字段上建立索引
2. 如果结果是需要保存的,就不使用temporary table, 减少内存争用

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表