博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【原创】Oracle实现PGSQL的generate_series
阅读量:6371 次
发布时间:2019-06-23

本文共 5234 字,大约阅读时间需要 17 分钟。

PostgreSQL 有生成序列的函数,可以方便的进行造数据。这里我用ORACLE实现了PGSQL的generate_series函数功能。

POSTGRESQL.

t_girl=# select * from generate_series(1,10); generate_series-----------------               1               2               3               4               5               6               7               8               9              10(10 行记录)时间:1.290 mst_girl=# select * from generate_series(1,10,2); generate_series-----------------               1               3               5               7               9(5 行记录)时间:0.431 mst_girl=# select * from generate_series(1,10,3); generate_series-----------------               1               4               7              10(4 行记录)时间:0.879 mst_girl=# select * from generate_series(2,10,3); generate_series-----------------               2               5               8(3 行记录)时间:0.867 mst_girl=# select count(*) from generate_series(1,1000); count-------  1000(1 行记录)时间:1.142 mst_girl=# select to_char(now() - '1 day'::interval * n,'YYYY-MM-DD') as result from generate_series(1,10)  g(n);   result------------ 2015-12-08 2015-12-07 2015-12-06 2015-12-05 2015-12-04 2015-12-03 2015-12-02 2015-12-01 2015-11-30 2015-11-29(10 行记录)时间:17.284 ms

ORACLE:

12:10:34 SQL> select * from table(ytt_generate_series(1,10));COLUMN_VALUE                                                                    ------------                                                                               1                                                                               2                                                                               3                                                                               4                                                                               5                                                                               6                                                                               7                                                                               8                                                                               9                                                                              10                                                                    已选择 10 行。已用时间:  00: 00: 00.0212:10:36 SQL> select * from table(ytt_generate_series(1,10,2));COLUMN_VALUE                                                                    ------------                                                                               1                                                                               3                                                                               5                                                                               7                                                                               9                                                                    已用时间:  00: 00: 00.0012:10:54 SQL> select * from table(ytt_generate_series(1,10,3));COLUMN_VALUE                                                                    ------------                                                                               1                                                                               4                                                                               7                                                                              10                                                                    已用时间:  00: 00: 00.0012:10:56 SQL> select * from table(ytt_generate_series(2,10,3));COLUMN_VALUE                                                                    ------------                                                                               2                                                                               5                                                                               8                                                                    已用时间:  00: 00: 00.0212:11:04 SQL> select count(*) from table(ytt_generate_series(1,1000));  COUNT(*)                                                                      ----------                                                                            1000                                                                      已用时间:  00: 00: 00.1313:32:13 SQL> select to_char(sysdate - n,'YYYY-MM-DD') from table(ytt_generate_series(1,10));TO_CHAR(SY----------2015-12-082015-12-072015-12-062015-12-052015-12-042015-12-032015-12-022015-12-012015-11-302015-11-29已选择 10 行。已用时间:  00: 00: 00.01

附上代码:

-- Declare result set.create or replace type ytt_num is object( n number);/create or replace type numbers_table is table of ytt_num;/-- Function body.-- Created by ytt.-- 2015/12/9create or replace function ytt_generate_series(f_start_num number := 1, -- Start number.f_end_num number,  -- Finish number.f_step_num number := 1 -- Step.)return numbers_table pipelinedis   list numbers_table := numbers_table();  i number := 0;  j number := 1;begin  i := f_start_num;  j := 1;  -- Increase nested table's size.  list.extend(f_end_num);  -- Loop begin.  while i <= f_end_num loop  -- Initlization.    list(j) := ytt_num(null);    list(j).n := i;    pipe row(list(j));    i := i + f_step_num;    j := j + 1;  end loop;  return;end;/

转载地址:http://syuqa.baihongyu.com/

你可能感兴趣的文章
二进制状态码
查看>>
Vue 中 CSS 动画原理
查看>>
关于 Promise 的 9 个提示
查看>>
算法复习
查看>>
安卓中高级开发面试知识点之——缓存
查看>>
Java的初始化顺序
查看>>
js 判断回文字符串
查看>>
shields小徽章是如何生成的?以及搭建自己的shield服务器
查看>>
猫头鹰的深夜翻译:spring事务管理
查看>>
记一次使用Spring REST Docs + travis + github自动生成API接口文档的操作步骤(下)...
查看>>
1、集合 2、Iterator迭代器 3、增强for循环 4、泛型
查看>>
关于/var/run/docker.sock
查看>>
SCrapy爬虫大战京东商城
查看>>
用 JavaScript 实现链表操作 - 11 Alternating Split
查看>>
Laravel优秀扩展包整理
查看>>
日志分析之识别真假蜘蛛与处理办法
查看>>
太多脚本将会毁掉持续交付
查看>>
一地鸡毛 OR 绝地反击,2019年区块链发展指南
查看>>
卢森堡大学发布RepuCoin系统,可破解区块链51%攻击
查看>>
国内云计算厂商众生相:四大阵营十几家企业生存盘点
查看>>