SQL SHOW系列(二)

简介:

Note

 The SQL take me almost half a day, which construct the data as a tree, an statistic data by the tree's full path, finally, show it in a tree.

 

SQL

 

 
  1. select LEVEL TREELEVEL, 
  2.        Z2.TYPE_NAME_SHOT, 
  3.        Z1.TOTAL_INVEST_NEW, 
  4.        Z1.YEAR_INVEST_NEW, 
  5.        Z1.CARRY_OVER_INVEST_NEW, 
  6.        Z1.TOTAL_INVEST_CON, 
  7.        Z1.YEAR_INVEST_CON, 
  8.        Z1.CARRY_OVER_INVEST_CON, 
  9.        Z1.TOTAL_INVEST_SUM, 
  10.        Z1.YEAR_INVEST_SUM, 
  11.        Z1.CARRY_OVER_INVEST_SUM 
  12.   from (SELECT TREE.BO_INVEST_FRAME_ID, 
  13.                TREE.NAME
  14.                SUM(x.TOTAL_INVEST_NEW) TOTAL_INVEST_NEW, 
  15.                SUM(x.YEAR_INVEST_NEW) YEAR_INVEST_NEW, 
  16.                SUM(x.CARRY_OVER_INVEST_NEW) CARRY_OVER_INVEST_NEW, 
  17.                SUM(x.TOTAL_INVEST_CON) TOTAL_INVEST_CON, 
  18.                SUM(x.YEAR_INVEST_CON) YEAR_INVEST_CON, 
  19.                SUM(x.CARRY_OVER_INVEST_CON) CARRY_OVER_INVEST_CON, 
  20.                SUM(x.TOTAL_INVEST_SUM) TOTAL_INVEST_SUM, 
  21.                SUM(x.YEAR_INVEST_SUM) YEAR_INVEST_SUM, 
  22.                SUM(x.CARRY_OVER_INVEST_SUM) CARRY_OVER_INVEST_SUM 
  23.           from ( 
  24.                 -- statitsc data 
  25.                 select SYS_CONNECT_BY_PATH(v.BO_INVEST_FRAME_ID, '^') FULLPATH, 
  26.                         LEVEL TREELEVEL, 
  27.                         V.type_name_shot, 
  28.                         v.bo_invest_frame_id, 
  29.                         v.parent_type_shot, 
  30.                         v.TOTAL_INVEST_NEW, 
  31.                         v.YEAR_INVEST_NEW, 
  32.                         v.CARRY_OVER_INVEST_NEW, 
  33.                         v.TOTAL_INVEST_CON, 
  34.                         v.YEAR_INVEST_CON, 
  35.                         v.CARRY_OVER_INVEST_CON, 
  36.                         v.TOTAL_INVEST_SUM, 
  37.                         v.YEAR_INVEST_SUM, 
  38.                         v.CARRY_OVER_INVEST_SUM 
  39.                   from (select v1.type_name_shot, 
  40.                                 v1.parent_type_shot, 
  41.                                 v1.bo_invest_frame_id, 
  42.                                 v1.sort_weight_shot, 
  43.                                 v2.*, 
  44.                                 v3.*, 
  45.                                 v4.* 
  46.                            from -- typic tree 
  47.                                  (SELECT t.bo_invest_frame_id, 
  48.                                          t.type_name_shot, 
  49.                                          t.parent_type_shot, 
  50.                                          t.sort_weight_shot 
  51.                                     FROM BO_INVEST_FRAME_SHOT t 
  52.                                    WHERE BO_INVEST_PLAN_ID = 
  53.                                          (select t.bo_invest_plan_id 
  54.                                             from bo_invest_plan_ver t 
  55.                                            where t.bo_invest_plan_ver_id = 
  56.                                                  'd53abea7-fa03-437a-91d0-cacaa0c49849'
  57.                                      AND INVEST_FRAME_TYPE = '1' 
  58.                                      AND TYPE_NAME_SHOT <> '其中:零星购置') v1, 
  59.                                 ( -- case one 
  60.                                  select tt.project_type, 
  61.                                          TO_CHAR(SUM(TT.TOTAL_INVEST), 
  62.                                                  'FM99999999999.99') TOTAL_INVEST_NEW, 
  63.                                          TO_CHAR(SUM(TT.YEAR_INVEST), 
  64.                                                  'FM99999999999.99') YEAR_INVEST_NEW, 
  65.                                          TO_CHAR(SUM(TT.CARRY_OVER_INVEST), 
  66.                                                  'FM99999999999.99') CARRY_OVER_INVEST_NEW 
  67.                                    from (select DECODE(t1.CARRY_OVER_FLAG, 
  68.                                                         '1'
  69.                                                         '结转项目'
  70.                                                         '2'
  71.                                                         '新开工项目') CARRY_OVER_FLAG_NAME, 
  72.                                                  t1.project_type, 
  73.                                                  t1.CARRY_OVER_FLAG, 
  74.                                                  t1.cancel_flag, 
  75.                                                  t1.delete_flag, 
  76.                                                  t1.bo_invest_plan_ver_id, 
  77.                                                  t1.carry_over_invest, 
  78.                                                  t1.total_invest, 
  79.                                                  t1.year_invest 
  80.                                             from bo_project_ver t1 
  81.                                            where t1.cancel_flag = '2' 
  82.                                              and t1.delete_flag = '2' 
  83.                                              and t1.parent_project = '0' 
  84.                                              and t1.bo_invest_plan_ver_id = 
  85.                                                  'd53abea7-fa03-437a-91d0-cacaa0c49849') TT 
  86.                                   where tt.CARRY_OVER_FLAG = '2' 
  87.                                   group by tt.project_type) v2, 
  88.                                 ( -- case two 
  89.                                  select tt.project_type, 
  90.                                          TO_CHAR(SUM(TT.TOTAL_INVEST), 
  91.                                                  'FM99999999999.99') TOTAL_INVEST_CON, 
  92.                                          TO_CHAR(SUM(TT.YEAR_INVEST), 
  93.                                                  'FM99999999999.99') YEAR_INVEST_CON, 
  94.                                          TO_CHAR(SUM(TT.CARRY_OVER_INVEST), 
  95.                                                  'FM99999999999.99') CARRY_OVER_INVEST_CON 
  96.                                    from (select DECODE(t1.CARRY_OVER_FLAG, 
  97.                                                         '1'
  98.                                                         '结转项目'
  99.                                                         '2'
  100.                                                         '新开工项目') CARRY_OVER_FLAG_NAME, 
  101.                                                  t1.project_type, 
  102.                                                  t1.CARRY_OVER_FLAG, 
  103.                                                  t1.cancel_flag, 
  104.                                                  t1.delete_flag, 
  105.                                                  t1.bo_invest_plan_ver_id, 
  106.                                                  t1.carry_over_invest, 
  107.                                                  t1.total_invest, 
  108.                                                  t1.year_invest 
  109.                                             from bo_project_ver t1 
  110.                                            where t1.cancel_flag = '2' 
  111.                                              and t1.delete_flag = '2' 
  112.                                              and t1.parent_project = '0' 
  113.                                              and t1.bo_invest_plan_ver_id = 
  114.                                                  'd53abea7-fa03-437a-91d0-cacaa0c49849') TT 
  115.                                   where tt.CARRY_OVER_FLAG = '1' 
  116.                                   group by tt.project_type) v3, 
  117.                                 ( -- case three 
  118.                                  select tt.project_type, 
  119.                                          TO_CHAR(SUM(TT.TOTAL_INVEST), 
  120.                                                  'FM99999999999.99') TOTAL_INVEST_SUM, 
  121.                                          TO_CHAR(SUM(TT.YEAR_INVEST), 
  122.                                                  'FM99999999999.99') YEAR_INVEST_SUM, 
  123.                                          TO_CHAR(SUM(TT.CARRY_OVER_INVEST), 
  124.                                                  'FM99999999999.99') CARRY_OVER_INVEST_SUM 
  125.                                    from (select DECODE(t1.CARRY_OVER_FLAG, 
  126.                                                         '1'
  127.                                                         '结转项目'
  128.                                                         '2'
  129.                                                         '新开工项目') CARRY_OVER_FLAG_NAME, 
  130.                                                  t1.project_type, 
  131.                                                  t1.CARRY_OVER_FLAG, 
  132.                                                  t1.cancel_flag, 
  133.                                                  t1.delete_flag, 
  134.                                                  t1.bo_invest_plan_ver_id, 
  135.                                                  t1.carry_over_invest, 
  136.                                                  t1.total_invest, 
  137.                                                  t1.year_invest 
  138.                                             from bo_project_ver t1 
  139.                                            where t1.cancel_flag = '2' 
  140.                                              and t1.delete_flag = '2' 
  141.                                              and t1.parent_project = '0' 
  142.                                              and t1.bo_invest_plan_ver_id = 
  143.                                                  'd53abea7-fa03-437a-91d0-cacaa0c49849') TT 
  144.                                   group by tt.project_type) v4 
  145.                           where v1.bo_invest_frame_id = v2.project_type(+) 
  146.                             and v1.bo_invest_frame_id = v3.project_type(+) 
  147.                             and v1.bo_invest_frame_id = v4.project_type(+)) V 
  148.                  START WITH V.PARENT_TYPE_SHOT = '0' 
  149.                 CONNECT BY PRIOR V.BO_INVEST_FRAME_ID = V.PARENT_TYPE_SHOT 
  150.                  ORDER SIBLINGS BY V.SORT_WEIGHT_SHOT) x, 
  151.                -- typic tree with full path 
  152.                (SELECT SYS_CONNECT_BY_PATH(T.BO_INVEST_FRAME_ID, '^') FULLPATH, 
  153.                        T.BO_INVEST_FRAME_ID ORGANIZATIONID, 
  154.                        T.BO_INVEST_FRAME_ID, 
  155.                        T.TYPE_NAME_SHOT NAME
  156.                        T.SORT_WEIGHT_SHOT, 
  157.                        T.PARENT_TYPE_SHOT PARENTORGANIZATIONID 
  158.                   FROM (SELECT * 
  159.                           FROM BO_INVEST_FRAME_SHOT 
  160.                          WHERE BO_INVEST_PLAN_ID = 
  161.                                (select t.bo_invest_plan_id 
  162.                                   from bo_invest_plan_ver t 
  163.                                  where t.bo_invest_plan_ver_id = 
  164.                                        'd53abea7-fa03-437a-91d0-cacaa0c49849'
  165.                            AND INVEST_FRAME_TYPE = '1' 
  166.                            AND TYPE_NAME_SHOT <> '其中:零星购置') T 
  167.                 CONNECT BY PRIOR T.BO_INVEST_FRAME_ID = T.PARENT_TYPE_SHOT 
  168.                  START WITH T.PARENT_TYPE_SHOT = '0' 
  169.                  ORDER SIBLINGS BY T.SORT_WEIGHT_SHOT) TREE 
  170.          WHERE x.FULLPATH LIKE TREE.FULLPATH || '%' 
  171.          GROUP BY TREE.BO_INVEST_FRAME_ID, TREE.NAME) Z1, 
  172.        --typic tree 
  173.        (SELECT T.BO_INVEST_FRAME_ID ORGANIZATIONID, 
  174.                T.BO_INVEST_FRAME_ID, 
  175.                T.TYPE_NAME_SHOT, 
  176.                T.SORT_WEIGHT_SHOT, 
  177.                T.PARENT_TYPE_SHOT 
  178.           FROM (SELECT * 
  179.                   FROM BO_INVEST_FRAME_SHOT 
  180.                  WHERE BO_INVEST_PLAN_ID = 
  181.                        (select t.bo_invest_plan_id 
  182.                           from bo_invest_plan_ver t 
  183.                          where t.bo_invest_plan_ver_id = 
  184.                                'd53abea7-fa03-437a-91d0-cacaa0c49849'
  185.                    AND INVEST_FRAME_TYPE = '1' 
  186.                    AND TYPE_NAME_SHOT <> '其中:零星购置') T) Z2 
  187.  where Z1.BO_INVEST_FRAME_ID = Z2.BO_INVEST_FRAME_ID 
  188. CONNECT BY PRIOR Z2.BO_INVEST_FRAME_ID = Z2.PARENT_TYPE_SHOT 
  189.  START WITH Z2.PARENT_TYPE_SHOT = '0' 
  190.  ORDER SIBLINGS BY Z2.SORT_WEIGHT_SHOT; 
  191.  /* 
  192.  * daniel zhou 2010-09-29 17:22
  193.  */ 

      

     本文转自danni505 51CTO博客,原文链接:http://blog.51cto.com/danni505/399242,如需转载请自行联系原作者


相关文章
|
4月前
|
SQL 存储 数据库
MySQL设计规约问题之性能分析工具如Sql explain、show profile和mysqlsla在数据库性能优化中有什么作用
MySQL设计规约问题之性能分析工具如Sql explain、show profile和mysqlsla在数据库性能优化中有什么作用
|
SQL 存储 监控
【MySQL从入门到精通】【高级篇】(二十二)慢查询日志分析,SHOW PROFILE查看SQL执行成本
上一篇文章我们介绍数据库的优化步骤【MySQL从入门到精通】【高级篇】(二十一)数据库优化步骤_查看系统性能参数,其中,说到了通过开启慢查询日志来分析慢查询的SQL。这篇文章就是具体来介绍如何开启慢查询日志以及如何分析慢查询日志。
415 0
【MySQL从入门到精通】【高级篇】(二十二)慢查询日志分析,SHOW PROFILE查看SQL执行成本
|
SQL 存储 关系型数据库
Mysql中 慢查询日志和show profile进行sql分析
MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中。
395 0
Mysql中 慢查询日志和show profile进行sql分析
|
SQL 存储 运维
用 Show Profile 进行 sql 分析|学习笔记
快速学习用 Show Profile 进行 sql 分析
128 0
用 Show Profile 进行 sql 分析|学习笔记
|
SQL 关系型数据库 数据库
开发指南—DAL语句—SHOW—慢SQL相关
本文介绍了慢SQL相关的SHOW语句。
105 0
|
SQL 关系型数据库 MySQL
几个必须掌握的SQL优化技巧(五):Show Profile分析SQL性能
在应用的开发过程中,由于开发初期的数据量一般都比较小,所以开发过程中一般都比较注重功能上的实现,但是当完成了一个应用或者系统之后,随着生产数据量的急剧增长,那么之前的很多sql语句的写法就会显现出一定的性能问题,对生产的影响也会越来越大,这些不恰当的sql语句就会成为整个系统性能的瓶颈,为了追求系统的极致性能,必须要对它们进行优化。
296 0
几个必须掌握的SQL优化技巧(五):Show Profile分析SQL性能
|
SQL 关系型数据库 数据库
开发指南—DAL语句—SHOW—慢SQL相关
本文介绍了慢SQL相关的SHOW语句。
|
SQL 关系型数据库 数据库
开发指南—DAL语句—SHOW—慢SQL相关
本文介绍了慢SQL相关的SHOW语句。
|
SQL 存储 算法
MySQL数据库性能优化由浅入深(表设计、慢查询、SQL索引优化、Explain分析、Show Profile分析、配置优化)
通俗地理解三个范式,对于数据库设计大有好处。在数据库设计中,为了更好地应用三个范式,就必须通俗地理解三个范式(通俗地理解是够用的理解,并不是最科学最准确的理解
481 0
MySQL数据库性能优化由浅入深(表设计、慢查询、SQL索引优化、Explain分析、Show Profile分析、配置优化)
|
存储 SQL 关系型数据库
PolarDB-X 1.0-SQL 手册-SHOW-规则和拓扑查询语句
规则和拓扑类语句如下: SHOW RULE [FROM [schemaname.]tablename] 语句 SHOW FULL RULE [FROM [schemaname.]tablename] 语句 SHOW TOPOLOGY FROM [schemaname.]tablename 语句 SHOW PARTITIONS FROM tablename 语句 SHOW BROADCASTS 语句 SHOW DATASOURCES 语句 SHOW NODE 语句
195 0