菜鸟学oracle - 用PL/SQL画直方图

菜鸟学oracle - 用PL/SQL画直方图[@more@]现在用的库里有一个MV是统计按小时的访问量的,数据如下


SQL> select * from mv_time_stat;

TIME CNT
---------- ----------
0 187
1 51
2 34
3 19
4 19
5 20
6 50
7 107
8 682
9 1342
10 1854
11 1292
12 1416
13 1180
14 1217
15 1573
16 1785
17 1469
18 1892
19 1907
20 1602
21 1540
22 1013
23 441

24 rows selected.


某年月日,突发奇想,打算在sqlplus下面按上面的数据画个直方图出来,首先写了个竖列的(因为好写):


SQL> COL Time FORMAT A7
SQL> SELECT LPAD(DECODE(TIME,TO_CHAR(SYSDATE,'HH24'),'NOW: '||TO_CHAR(TIME),TO_CHAR(TIME)),7,' ') AS "Time",
2 SUBSTR('|'||LPAD('> '||CNT,CEIL(CNT/A.TOTAL*300)+LENGTH(TO_CHAR(CNT))+1,'-'),1,35) AS " Count per hour"
3 FROM MV_TIME_STAT,(SELECT SUM(CNT) AS TOTAL FROM MV_TIME_STAT) A;
COL Time CLEAR

Time Count per hour
------- ----------------------------------------------------------------------
0 |--&gt 187
1 |> 51
2 |> 34
3 |> 19
4 |> 19
5 |> 20
6 |> 50
7 |-> 107
NOW: 8 |---------&gt 682
9 |-----------------&gt 1342
10 |------------------------&gt 1854
11 |-----------------&gt 1292
12 |------------------&gt 1416
13 |---------------&gt 1180
14 |----------------&gt 1217
15 |--------------------&gt 1573
16 |-----------------------&gt 1785
17 |-------------------&gt 1469
18 |-------------------------&gt 1892
19 |-------------------------&gt 1907
20 |---------------------&gt 1602
21 |--------------------&gt 1540
22 |-------------&gt 1013
23 |-----&gt 441

24 rows selected.


然后又想,怎么样把它横过来,到网上查资料,又突击学习了下分析函数,写了一个过程是这个样子的:


CREATE OR REPLACE PROCEDURE HISTOGRAPH(HEIGHT NUMBER DEFAULT 15) AS
MAX_ONE NUMBER;
STR_LINE VARCHAR(120);
STR_TEMP VARCHAR(120);
I NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE(LPAD('^',14,' '));
SELECT MAX(CNT) INTO MAX_ONE FROM MV_TIME_STAT;
FOR I IN 1 .. HEIGHT+1 LOOP
STR_LINE:=LPAD(TO_CHAR(MAX_ONE-(I-1)*MAX_ONE/HEIGHT,'99999')||'-'||TO_CHAR(MAX_ONE-(I-2)*MAX_ONE/HEIGHT,'99999')||'|',14,'0');
SELECT MAX(SYS_CONNECT_BY_PATH(
DECODE(SIGN(CNT-MAX_ONE+(I-1)*MAX_ONE/HEIGHT),-1,
DECODE(SIGN(CNT-MAX_ONE+(I-2)*MAX_ONE/HEIGHT),-1,'^','*'),
'|'),
' ')) INTO STR_TEMP
FROM MV_TIME_STAT
START WITH TIME=0
CONNECT BY PRIOR TIME=TIME-1;
STR_LINE:=STR_LINE||STR_TEMP;
DBMS_OUTPUT.PUT_LINE(REPLACE (STR_LINE,'^',' '));
END LOOP;
SELECT MAX(SYS_CONNECT_BY_PATH(TO_CHAR(TIME,'00'),'^')) INTO STR_TEMP
FROM MV_TIME_STAT
START WITH TIME=0
CONNECT BY PRIOR TIME=TIME-1;
DBMS_OUTPUT.PUT_LINE(LPAD('0',14,' ')||LPAD('>',LENGTH(REPLACE(STR_TEMP,' '))+3,'-'));
DBMS_OUTPUT.PUT_LINE(REPLACE(LPAD('^',14,' ')||REPLACE(STR_TEMP,' '),'^',' '));
END;
/

Procedure created.

SQL> exec histograph;
^
1907- 2034| |
1780- 1907| | | | |
1653- 1780| | | | |
1526- 1653| | | | | | | |
1398- 1526| | | | | | | | | |
1271- 1398| | | | | | | | | | | |
1144- 1271| | | | | | | | | | | | | |
1017- 1144| | | | | | | | | | | | | |
890- 1017| | | | | | | | | | | | | | |
763- 890| | | | | | | | | | | | | | |
636- 763| | | | | | | | | | | | | | | |
509- 636| | | | | | | | | | | | | | | |
381- 509| | | | | | | | | | | | | | | | |
254- 381| | | | | | | | | | | | | | | | |
127- 254| | | | | | | | | | | | | | | | | |
0- 127| | | | | | | | | | | | | | | | | | | | | | | | |
0--------------------------------------------------------------------------&gt
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23

PL/SQL procedure successfully completed.


自己感觉应该还可以用一条语句实现,不过偷懒了。。

发上来大家共享,顺便抛砖引玉,各位大牛不吝赐教。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/594576/viewspace-934567/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/594576/viewspace-934567/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值