如何执行查询输出

时间:2019-05-07 15:35:02

标签: oracle plsql

我们编写了一个sql,该sql返回模式中存在的数字列的总和和平均值。

如果可以在单个查询中一起执行查询和queryoutput,请提供帮助。我们没有创建或插入权限。

select 'select  avg(' || column_name ||'), sum('||column_name|| '), '|| table_name 
       ||' from '|| table_name ||'   ' ||'union all' 
from all_tab_columns
where data_type= 'NUMBER'
and owner not in ('SYS','PUBLIC','WMSYS','SYSTEM') 
and column_name not in ('BATCHNUM')

以上查询的输出为

select  avg(amt), sum(amt2), table1 from table1   
union all
select  avg(amt2), sum(amt5), table2 from table2  

这是我们要在单个查询中实现的目标

schema Tablename  columname   average  sum 
Test    testable   amount       10      1000
Test    testable   amounttrans  100      4000
Test2   transtable  amount       100     5000

谢谢

1 个答案:

答案 0 :(得分:0)

您可以创建动态视图(如果您没有权限,请让您的超级用户/ dba为您创建视图)

在此示例中,我将使用HR模式中的几行。您也可以使用all_tab_columns

对其进行修改以添加模式名称
DECLARE
     v_sql CLOB := empty_clob();
    BEGIN

    for rec IN (select 'select '''||table_name||''' as tablename,
         '''||column_name||''' as columname,
                   avg(' || column_name ||') as average,sum('
                         || column_name || ') as "SUM" from '
                         || table_name
                         ||'   ' as qry
                  FROM user_tab_columns --all_tab_columns
    where data_type= 'NUMBER'
    --and owner not in ('SYS','PUBLIC','WMSYS','SYSTEM') 
                        AND column_name NOT IN ('BATCHNUM'
                  )
                        AND ROWNUM < 10
                ) LOOP v_sql := v_sql
                                || rec.qry
                                || ' UNION ALL ';
      END LOOP;
        v_sql := regexp_replace(v_sql,' UNION ALL $');
     EXECUTE IMMEDIATE 'CREATE OR REPLACE view my_view as ' || v_sql;
END;
/

现在,查询视图。

SQL> set sqlformat ansiconsole
SQL> select * from my_view;
TABLENAME     COLUMNAME                                           AVERAGE      SUM
REGIONS       REGION_ID                                               2.5       10
COUNTRIES     REGION_ID                                               2.4       60
LOCATIONS     LOCATION_ID                                            2100    48300
DEPARTMENTS   DEPARTMENT_ID                                           140     3780
DEPARTMENTS   MANAGER_ID         154.909090909090909090909090909090909091     1704
DEPARTMENTS   LOCATION_ID       1777.777777777777777777777777777777777778    48000
JOBS          MIN_SALARY        6573.052631578947368421052631578947368421   124888
JOBS          MAX_SALARY         13215.1578947368421052631578947368421053   251088
EMPLOYEES     EMPLOYEE_ID                                             153    16371

如果您运行的是Oracle 12c及更高版本,则可以使用DBMS_SQL.RETURN_RESULT

使用上述相同的块,除了这些更改。

DECLARE
 ..
 v_cur SYS_REFCURSOR;
  BEGIN
  ...
  END LOOP;
    v_sql := regexp_replace(v_sql,' UNION ALL $');
     OPEN v_cur for v_sql;
    DBMS_SQL.RETURN_RESULT(v_cur);
  END;
/ 


ResultSet #1


TABLENAME   COLUMNAME        AVERAGE        SUM
----------- ------------- ---------- ----------
REGIONS     REGION_ID            2.5         10
COUNTRIES   REGION_ID            2.4         60
LOCATIONS   LOCATION_ID         2100      48300
DEPARTMENTS DEPARTMENT_ID        140       3780
DEPARTMENTS MANAGER_ID    154.909091       1704
DEPARTMENTS LOCATION_ID   1777.77778      48000
JOBS        MIN_SALARY    6573.05263     124888
JOBS        MAX_SALARY    13215.1579     251088
EMPLOYEES   EMPLOYEE_ID          153      16371

9 rows selected.