WEB开发网
开发学院数据库Oracle 菜鸟学oracle - 用PL/SQL画直方图 阅读

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

 2007-05-12 12:22:48 来源:WEB开发网   
核心提示: 然后又想,怎么样把它横过来,到网上查资料,又突击学习了下分析函数,写了一个过程是这个样子的:代码:CREATE OR REPLACE PROCEDURE HISTOGRAPH(HEIGHT NUMBER DEFAULT 15) ASMAX_ONE NUMBER;STR_LINE VARCH

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

代码:
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-------------------------------------------------------------------------->
       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.

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

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

上一页  1 2 

Tags:菜鸟 oracle PL

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接