Oracle 特殊函数 报表格式 [实例]

Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as ZLCHS
 
SQL>
SQL> -------------首列的汇总
SQL> Select b.简称, To_Char(a.登记时间, 'YYYY-MM') As 日期, Sum(a.实收金额) As 实收
  2  From 个人费用记录 a, 部门 b
  3  Where a.开单部门id = b.资源id
  4  Group By Rollup(b.简称, To_Char(a.登记时间, 'YYYY-MM'));
 
简称                                                       日期          实收
------------------------------------------------------------ ------- ----------
CT室                                                      2010-07       49.8
CT室                                                                    49.8
儿科                                                       2010-07     4143.9
儿科                                                       2010-08     1859.6
儿科                                                       2010-09         -6
儿科                                                                   5997.5
妇科                                                       2010-07    5790.38
妇科                                                       2010-08     1943.7
妇科                                                                  7734.08
内科                                                       2010-07    18966.7
内科                                                       2010-08    -7522.3
内科                                                                  11444.4
医护                                                       2010-09      132.5
医护                                                                    132.5
检查科                                                   2010-08     2117.2
检查科                                                               2117.2
治疗室                                                   2010-07      240.8
治疗室                                                   2010-08    2215.25
治疗室                                                   2010-09     4415.3
治疗室                                                   2010-10        9.9
 
简称                                                       日期          实收
------------------------------------------------------------ ------- ----------
治疗室                                                              6881.25
中医科                                                   2010-09       7832
中医科                                                   2010-10      328.2
中医科                                                               8160.2
方案中心                                                            2263.8
方案中心                                                2010-08          8
方案中心                                                2010-09     1524.2
方案中心                                                2010-10   93739.71
方案中心                                                2010-11   53001.78
方案中心                                                         150537.49
社区内科                                                2010-08     4880.2
社区内科                                                2010-09     1708.9
社区内科                                                2010-10     2455.5
社区内科                                                            9044.6
社区方案组                                             2010-07      110.1
社区方案组                                             2010-09        115
社区方案组                                                          225.1
社区西药房                                             2010-08      929.5
社区西药房                                             2010-09       13.4
社区西药房                                                          942.9
社区西药库                                             2010-08      165.8
 
简称                                                       日期          实收
------------------------------------------------------------ ------- ----------
社区西药库                                             2010-09        6.3
社区西药库                                             2010-10
社区西药库                                                          172.1
                                                                      203439.12
 
45 rows selected
SQL> -------------增加日期列的汇总
SQL> Select b.简称, To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM') As 日期, Sum(a.实收金额) As 实收
  2  From 个人费用记录 a, 部门 b
  3  Where a.开单部门id = b.资源id
  4  Group By Cube(b.简称, To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM'))
  5  Order By 1, 2;
 
简称                                                       日期          实收
------------------------------------------------------------ ------- ----------
CT室                                                      2010-07       49.8
CT室                                                                    49.8
儿科                                                       2010-07     4143.9
儿科                                                       2010-08     1859.6
儿科                                                       2010-09         -6
儿科                                                                   5997.5
方案中心                                                2010-08          8
方案中心                                                2010-09     1524.2
方案中心                                                2010-10   93739.71
方案中心                                                2010-11   55265.58
方案中心                                                         150537.49
妇科                                                       2010-07    5790.38
妇科                                                       2010-08     1943.7
妇科                                                                  7734.08
检查科                                                   2010-08     2117.2
检查科                                                               2117.2
内科                                                       2010-07    18966.7
内科                                                       2010-08    -7522.3
内科                                                                  11444.4
社区方案组                                             2010-07      110.1
 
简称                                                       日期          实收
------------------------------------------------------------ ------- ----------
社区方案组                                             2010-09        115
社区方案组                                                          225.1
社区内科                                                2010-08     4880.2
社区内科                                                2010-09     1708.9
社区内科                                                2010-10     2455.5
社区内科                                                            9044.6
社区西药房                                             2010-08      929.5
社区西药房                                             2010-09       13.4
社区西药房                                                          942.9
社区西药库                                             2010-08      165.8
社区西药库                                             2010-09        6.3
社区西药库                                             2010-10
社区西药库                                                          172.1
医护                                                       2010-09      132.5
医护                                                                    132.5
治疗室                                                   2010-07      240.8
治疗室                                                   2010-08    2215.25
治疗室                                                   2010-09     4415.3
治疗室                                                   2010-10        9.9
治疗室                                                              6881.25
中医科                                                   2010-09       7832
 
简称                                                       日期          实收
------------------------------------------------------------ ------- ----------
中医科                                                   2010-10      328.2
中医科                                                               8160.2
                                                             2010-07   29301.68
                                                             2010-08    6596.95
                                                             2010-09    15741.6
                                                             2010-10   96533.31
                                                             2010-11   55265.58
                                                                      203439.12
 
49 rows selected
SQL> --------------------  Grouping_id   --Grouping
SQL> Select m.标识, m.标志, m.开单科室, m.日期, m.实收
  2  From (Select Grouping_Id(b.简称, To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM')) As 标识, Decode(a.记录性质, 1, '门诊', '住院') As 标志, b.简称 As 开单科室,
  3                To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM') As 日期, Sum(a.实收金额) As 实收
  4         From 个人费用记录 a, 部门 b
  5         Where a.开单部门id = b.资源id
  6         Group By Cube(b.简称, To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM')), Decode(a.记录性质, 1, '门诊', '住院')) m;
 
        标识 标志 开单科室                                                日期          实收
---------- ---- ------------------------------------------------------------ ------- ----------
         3 门诊                                                                       167819.01
         2 门诊                                                              2010-07   29301.68
         2 门诊                                                              2010-08       12.3
         2 门诊                                                              2010-09       4899
         2 门诊                                                              2010-10   78340.45
         2 门诊                                                              2010-11   55265.58
         1 门诊 CT室                                                                    49.8
         0 门诊 CT室                                                      2010-07       49.8
         1 门诊 儿科                                                                   5997.5
         0 门诊 儿科                                                       2010-07     4143.9
         0 门诊 儿科                                                       2010-08     1859.6
         0 门诊 儿科                                                       2010-09         -6
         1 门诊 妇科                                                                  7734.08
         0 门诊 妇科                                                       2010-07    5790.38
         0 门诊 妇科                                                       2010-08     1943.7
         1 门诊 内科                                                                  11444.4
         0 门诊 内科                                                       2010-07    18966.7
         0 门诊 内科                                                       2010-08    -7522.3
         1 门诊 医护                                                                      2.3
         0 门诊 医护                                                       2010-09        2.3
 
        标识 标志 开单科室                                                日期          实收
---------- ---- ------------------------------------------------------------ ------- ----------
         1 门诊 治疗室                                                                  897
         0 门诊 治疗室                                                   2010-07      240.8
         0 门诊 治疗室                                                   2010-09      646.3
         0 门诊 治疗室                                                   2010-10        9.9
         1 门诊 中医科                                                               1277.8
         0 门诊 中医科                                                   2010-09       1067
         0 门诊 中医科                                                   2010-10      210.8
         1 门诊 方案中心                                                         134446.63
         0 门诊 方案中心                                                2010-09     1474.2
         0 门诊 方案中心                                                2010-10   77706.85
         0 门诊 方案中心                                                2010-11   55265.58
         1 门诊 社区内科                                                            5853.1
         0 门诊 社区内科                                                2010-08     3731.3
         0 门诊 社区内科                                                2010-09     1708.9
         0 门诊 社区内科                                                2010-10      412.9
         1 门诊 社区方案组                                                          110.1
         0 门诊 社区方案组                                             2010-07      110.1
         1 门诊 社区西药房                                                              0
         0 门诊 社区西药房                                             2010-08          0
         1 门诊 社区西药库                                                            6.3
         0 门诊 社区西药库                                             2010-09        6.3
 
        标识 标志 开单科室                                                日期          实收
---------- ---- ------------------------------------------------------------ ------- ----------
         3 住院                                                                        35620.11
         2 住院                                                              2010-08    6584.65
         2 住院                                                              2010-09    10842.6
         2 住院                                                              2010-10   18192.86
         1 住院 医护                                                                    130.2
         0 住院 医护                                                       2010-09      130.2
         1 住院 检查科                                                               2117.2
         0 住院 检查科                                                   2010-08     2117.2
         1 住院 治疗室                                                              5984.25
         0 住院 治疗室                                                   2010-08    2215.25
         0 住院 治疗室                                                   2010-09       3769
         1 住院 中医科                                                               6882.4
         0 住院 中医科                                                   2010-09       6765
         0 住院 中医科                                                   2010-10      117.4
         1 住院 方案中心                                                          16090.86
         0 住院 方案中心                                                2010-08          8
         0 住院 方案中心                                                2010-09         50
         0 住院 方案中心                                                2010-10   16032.86
         1 住院 社区内科                                                            3191.5
         0 住院 社区内科                                                2010-08     1148.9
         0 住院 社区内科                                                2010-09          0
 
        标识 标志 开单科室                                                日期          实收
---------- ---- ------------------------------------------------------------ ------- ----------
         0 住院 社区内科                                                2010-10     2042.6
         1 住院 社区方案组                                                            115
         0 住院 社区方案组                                             2010-09        115
         1 住院 社区西药房                                                          942.9
         0 住院 社区西药房                                             2010-08      929.5
         0 住院 社区西药房                                             2010-09       13.4
         1 住院 社区西药库                                                          165.8
         0 住院 社区西药库                                             2010-08      165.8
         0 住院 社区西药库                                             2010-10
 
71 rows selected
SQL> -------------------★美化报表★
SQL> Select Decode(Grouping(b.简称), 1, '总计', 简称) As 科室, Decode(Grouping(To_Char(a.登记时间, 'YYYY-MM')), 1, '小计', To_Char(a.登记时间, 'YYYY-MM')) 日期,
  2         Sum(a.实收金额) As 实收
  3  From 个人费用记录 a, 部门 b
  4  Where a.开单部门id = b.资源id Having Sum(a.实收金额) <> 0
  5  Group By Rollup(b.简称, (b.简称, To_Char(a.登记时间, 'YYYY-MM')))
  6  Order By 1,2;
 
科室                                                       日期          实收
------------------------------------------------------------ ------- ----------
CT室                                                      2010-07       49.8
CT室                                                      小计        49.8
儿科                                                       2010-07     4143.9
儿科                                                       2010-08     1859.6
儿科                                                       2010-09         -6
儿科                                                       小计      5997.5
方案中心                                                2010-08          8
方案中心                                                2010-09     1524.2
方案中心                                                2010-10   93739.71
方案中心                                                2010-11   53001.78
方案中心                                                小计   150537.49
方案中心                                                            2263.8
妇科                                                       2010-07    5790.38
妇科                                                       2010-08     1943.7
妇科                                                       小计     7734.08
检查科                                                   2010-08     2117.2
检查科                                                   小计      2117.2
内科                                                       2010-07    18966.7
内科                                                       2010-08    -7522.3
内科                                                       小计     11444.4
 
科室                                                       日期          实收
------------------------------------------------------------ ------- ----------
社区方案组                                             2010-07      110.1
社区方案组                                             2010-09        115
社区方案组                                             小计       225.1
社区内科                                                2010-08     4880.2
社区内科                                                2010-09     1708.9
社区内科                                                2010-10     2455.5
社区内科                                                小计      9044.6
社区西药房                                             2010-08      929.5
社区西药房                                             2010-09       13.4
社区西药房                                             小计       942.9
社区西药库                                             2010-08      165.8
社区西药库                                             2010-09        6.3
社区西药库                                             小计       172.1
医护                                                       2010-09      132.5
医护                                                       小计       132.5
治疗室                                                   2010-07      240.8
治疗室                                                   2010-08    2215.25
治疗室                                                   2010-09     4415.3
治疗室                                                   2010-10        9.9
治疗室                                                   小计     6881.25
中医科                                                   2010-09       7832
 
科室                                                       日期          实收
------------------------------------------------------------ ------- ----------
中医科                                                   2010-10      328.2
中医科                                                   小计      8160.2
总计                                                       小计   203439.12
 
44 rows selected
SQL>          ----------------------★排行<不跳过方式>
SQL>            Select b.简称 As 科室, Sum(a.实收金额) As 实收金额, Rank() Over(Order By Sum(a.实收金额) Desc) 实收排行
  2             From 个人费用记录 a, 部门 b
  3             Where a.开单部门id = b.资源id Having Sum(a.实收金额) <> 0
  4             Group By b.简称;
 
科室                                                             实收金额       实收排行
------------------------------------------------------------ ---------- ----------
方案中心                                                 150537.49          1
内科                                                          11444.4          2
社区内科                                                    9044.6          3
中医科                                                       8160.2          4
妇科                                                          7734.08          5
治疗室                                                      6881.25          6
儿科                                                           5997.5          7
检查科                                                       2117.2          8
社区西药房                                                  942.9          9
社区方案组                                                  225.1         10
社区西药库                                                  172.1         11
医护                                                            132.5         12
CT室                                                            49.8         13
 
13 rows selected
SQL> ----------------------比例划分
SQL> Select b.简称 As 科室, Sum(a.实收金额) As 实收金额, Ntile(4) Over(Order By Sum(a.实收金额) Desc) 所属区段
  2  From 个人费用记录 a, 部门 b
  3  Where a.开单部门id = b.资源id Having Sum(a.实收金额) <> 0
  4  Group By b.简称;
 
科室                                                             实收金额       所属区段
------------------------------------------------------------ ---------- ----------
方案中心                                                 150537.49          1
内科                                                          11444.4          1
社区内科                                                    9044.6          1
中医科                                                       8160.2          1
妇科                                                          7734.08          2
治疗室                                                      6881.25          2
儿科                                                           5997.5          2
检查科                                                       2117.2          3
社区西药房                                                  942.9          3
社区方案组                                                  225.1          3
社区西药库                                                  172.1          4
医护                                                            132.5          4
CT室                                                            49.8          4
 
13 rows selected
SQL> --------------------所占份额
SQL> Select b.简称 As 科室, Sum(a.实收金额) As 实收金额, Ntile(4) Over(Order By Sum(a.实收金额) Desc) 所属区段, 100 * Round(Sum(a.实收金额) / Sum(Sum(a.实收金额)) Over(), 4) "份额(%)"
  2  From 个人费用记录 a, 部门 b
  3  Where a.开单部门id = b.资源id Having Sum(a.实收金额) <> 0
  4  Group By b.简称;
 
科室                                                             实收金额       所属区段      份额(%)
------------------------------------------------------------ ---------- ---------- ----------
方案中心                                                 150537.49          1         74
内科                                                          11444.4          1       5.63
社区内科                                                    9044.6          1       4.45
中医科                                                       8160.2          1       4.01
妇科                                                          7734.08          2        3.8
治疗室                                                      6881.25          2       3.38
儿科                                                           5997.5          2       2.95
检查科                                                       2117.2          3       1.04
社区西药房                                                  942.9          3       0.46
社区方案组                                                  225.1          3       0.11
社区西药库                                                  172.1          4       0.08
医护                                                            132.5          4       0.07
CT室                                                            49.8          4       0.02
 
13 rows selected
SQL> -----累计平均金额
SQL> Select To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM') As 月份, Sum(a.实收金额) As 科室实收,
  2         Avg(Sum(a.实收金额)) Over(Order By To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM') Rows Between Unbounded Preceding And Current Row) 累计平均金额
  3  From 个人费用记录 a, 部门 b
  4  Where a.开单部门id = b.资源id Having Sum(a.实收金额) <> 0
  5  Group By To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM')
  6  Order By To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM');
 
月份        科室实收       累计平均金额
------- ---------- ------------
2010-07   29301.68     29301.68
2010-08    6596.95    17949.315
2010-09    15741.6     17213.41
2010-10   96533.31    37043.385
2010-11   55265.58    40687.824
SQL> -----累计销量金额
SQL> Select To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM') As 月份, Sum(a.实收金额) As 科室实收,
  2         Sum(Sum(a.实收金额)) Over(Order By To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM') Rows Between Unbounded Preceding And Current Row) 累计合计金额
  3  From 个人费用记录 a, 部门 b
  4  Where a.开单部门id = b.资源id Having Sum(a.实收金额) <> 0
  5  Group By To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM')
  6  Order By to_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM');
 
月份        科室实收       累计合计金额
------- ---------- ------------
2010-07   29301.68     29301.68
2010-08    6596.95     35898.63
2010-09    15741.6     51640.23
2010-10   96533.31    148173.54
2010-11   55265.58    203439.12
SQL> -------前后三个月平均收入
SQL> Select To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM') As 月份, Sum(a.实收金额) As 科室实收,
  2         Round(Avg(Sum(a.实收金额)) Over(Order By To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM') Rows Between 1 Preceding And 1 Following), 2) 金额
  3  From 个人费用记录 a, 部门 b
  4  Where a.开单部门id = b.资源id Having Sum(a.实收金额) <> 0
  5  Group By To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM')
  6  Order By To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM');
 
月份        科室实收         金额
------- ---------- ----------
2010-07   29301.68   17949.32
2010-08    6596.95   17213.41
2010-09    15741.6   39623.95
2010-10   96533.31   55846.83
2010-11   55265.58   75899.45
SQL> -------前后三个月的合计收入
SQL> Select To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM') As 月份, Sum(a.实收金额) As 科室实收,
  2         Round(Sum(Sum(a.实收金额)) Over(Order By To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM') Rows Between 1 Preceding And 1 Following), 2) 金额
  3  From 个人费用记录 a, 部门 b
  4  Where a.开单部门id = b.资源id Having Sum(a.实收金额) <> 0
  5  Group By To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM')
  6  Order By To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM');
 
月份        科室实收         金额
------- ---------- ----------
2010-07   29301.68   35898.63
2010-08    6596.95   51640.23
2010-09    15741.6  118871.86
2010-10   96533.31  167540.49
2010-11   55265.58  151798.89
SQL> ----------------------
SQL> Select b.简称 As 科室, To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM') As 日期, Sum(a.实收金额) As 实收金额
  2  From 个人费用记录 a, 部门 b
  3  Where a.开单部门id = b.资源id Having Sum(a.实收金额) <> 0
  4  Group By Grouping Sets(b.简称, To_Char(Nvl(a.登记时间, Sysdate), 'YYYY-MM'));
 
科室                                                       日期        实收金额
------------------------------------------------------------ ------- ----------
内科                                                                  11444.4
CT室                                                                    49.8
社区西药房                                                          942.9
中医科                                                               8160.2
妇科                                                                  7734.08
医护                                                                    132.5
儿科                                                                   5997.5
社区内科                                                            9044.6
检查科                                                               2117.2
方案中心                                                         150537.49
治疗室                                                              6881.25
社区西药库                                                          172.1
社区方案组                                                          225.1
                                                             2010-08    6596.95
                                                             2010-07   29301.68
                                                             2010-10   96533.31
                                                             2010-09    15741.6
                                                             2010-11   55265.58
 
18 rows selected
 
SQL>

你可能感兴趣的:(oracle,sql,database,报表,2010)