【SQL*Plus】使用BREAK和COMPUTE在SQL*Plus中得到分組統計結果

secooler發表於2009-11-07
秉承KISS原則,一切簡單實用的工具都是值得深入挖掘的。SQL*Plus就是其中之一。我透過這個小文兒給大家介紹一下在SQL*Plus中使用break和compute命令實現類似group分組函式的統計效果,這裡只是拋個“磚”,期待大家“玉”的到來。

1.建立樣例表t,並簡單初始化幾條資料,以便後續的演示操作。
sec@ora10g> create table t (x number, y varchar2(20), z number);
sec@ora10g> insert into t values (1, 'a', 100);
sec@ora10g> insert into t values (2, 'b', 200);
sec@ora10g> insert into t values (2, 'c', 300);
sec@ora10g> insert into t values (3, 'd', 400);
sec@ora10g> insert into t values (3, 'e', 500);
sec@ora10g> insert into t values (3, 'f', 600);
sec@ora10g> insert into t values (4, 'g', 700);
sec@ora10g> insert into t values (4, 'h', 800);
sec@ora10g> insert into t values (4, 'i', 1000);
sec@ora10g> insert into t values (4, 'j', 1100);

2.最普通的查詢結果,這個最簡單的select語句聽說地球人都知道了。
sec@ora10g> select * From t;

         X Y                             Z
---------- -------------------- ----------
         1 a                           100
         2 b                           200
         2 c                           300
         3 d                           400
         3 e                           500
         3 f                           600
         4 g                           700
         4 h                           800
         4 i                          1000
         4 j                          1100

10 rows selected.

3.引入BREAK命令,請看select語句的輸出效果,不說自明,BREAK作用是將x列重複的值進行隱藏,方便檢視。似乎使用過BREAK命令的地球人不是很多。
sec@ora10g> break on x
sec@ora10g> select * From t;

         X Y                             Z
---------- -------------------- ----------
         1 a                           100
         2 b                           200
           c                           300
         3 d                           400
           e                           500
           f                           600
         4 g                           700
           h                           800
           i                          1000
           j                          1100

10 rows selected.

4.在上面的基礎上再引入COMPUTE命令。“計算”效果明顯,實現了對X列分組求和的目的。
sec@ora10g> compute sum of z on x;
sec@ora10g> select * From t;

         X Y                             Z
---------- -------------------- ----------
         1 a                           100
**********                      ----------
sum                                    100
         2 b                           200
           c                           300
**********                      ----------
sum                                    500
         3 d                           400
           e                           500
           f                           600
**********                      ----------
sum                                   1500
         4 g                           700
           h                           800
           i                          1000
           j                          1100
**********                      ----------
sum                                   3600

10 rows selected.

5.到此,分組求和的功能實現了,但是顯示效果有點壓抑,我們再使用“skip 1”命令在每組結果後面新增一空行(如果想加入2個空行可以使用“skip 2”,以此類推……)。
sec@ora10g> break on x skip 1
sec@ora10g> select * From t;

         X Y                             Z
---------- -------------------- ----------
         1 a                           100
**********                      ----------
sum                                    100

         2 b                           200
           c                           300
**********                      ----------
sum                                    500

         3 d                           400
           e                           500
           f                           600
**********                      ----------
sum                                   1500

         4 g                           700
           h                           800
           i                          1000
           j                          1100
**********                      ----------
sum                                   3600


10 rows selected.

6.如果行數眾多,在顯示結果的尾部將無法看到每一列的列名資訊,此時“skip page 1”命令就派上用場了,請看下面的效果。
sec@ora10g> break on x skip page 1
sec@ora10g> select * From t;

         X Y                             Z
---------- -------------------- ----------
         1 a                           100
**********                      ----------
sum                                    100

         X Y                             Z
---------- -------------------- ----------
         2 b                           200
           c                           300
**********                      ----------
sum                                    500

         X Y                             Z
---------- -------------------- ----------
         3 d                           400
           e                           500
           f                           600
**********                      ----------
sum                                   1500

         X Y                             Z
---------- -------------------- ----------
         4 g                           700
           h                           800
           i                          1000
           j                          1100
**********                      ----------
sum                                   3600

10 rows selected.

7.上面的實驗結果已經滿足我們僅使用SQL*Plus命令來完成類似group by分組求和的目的。一顆追求深入的心在不停的追問:“SQL*Plus中還有哪些類似sum的“分組函式”可用呢?”請
看我繼續分解
sec@ora10g> compute sum minimum maximum avg std variance count number of z on x;
sec@ora10g> select * From t;

         X Y                             Z
---------- -------------------- ----------
         1 a                           100
**********                      ----------
avg                                    100
count                                    1
minimum                                100
maximum                                100
number                                   1
sum                                    100
std                                      0
variance                                 0

         X Y                             Z
---------- -------------------- ----------
         2 b                           200
           c                           300
**********                      ----------
avg                                    250
count                                    2
minimum                                200
maximum                                300
number                                   2
sum                                    500
std                             70.7106781
variance                              5000

         X Y                             Z
---------- -------------------- ----------
         3 d                           400
           e                           500
           f                           600
**********                      ----------
avg                                    500
count                                    3
minimum                                400
maximum                                600
number                                   3
sum                                   1500
std                                    100
variance                             10000

         X Y                             Z
---------- -------------------- ----------
         4 g                           700
           h                           800
           i                          1000
           j                          1100
**********                      ----------
avg                                    900
count                                    4
minimum                                700
maximum                               1100
number                                   4
sum                                   3600
std                             182.574186
variance                        33333.3333

10 rows selected.

上面的結果就是在SQL*Plus中能夠得到統計功能,這裡再逐條分解一下:
avg      :Average of the values in the column.(求平均值)
count    :Number of non-null values in the column.(統計這組中一共有多少條非空記錄)
minimum  :Minimum value in the column.(選出這組值中最小的一個)
maximum  :Maximum value in the column.(選出這組值中最大的一個)
number   :Number of rows in the column.(統計這組中一共有多少條記錄,包含空記錄)
sum      :Sum of the values in the column.(求和)
std      :Standard deviation of the values in the column.(求標準差)
variance :Variance of the values in the column.(求方差)


8.小結
基於上面演示的BREAK和COMPUTE命令所實現的功能,SQL*Plus的強大之處可見一斑。
其實,單純使用SQL*Plus就可以生成一份非常精美的報表,有興趣的朋友可以繼續深入挖掘一下,其樂無窮也。
“拋磚”到此結束,現在是“引玉”時間……

-- The End --

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-618344/,如需轉載,請註明出處,否則將追究法律責任。

相關文章