在一个匿名块中组合代码

时间:2012-11-18 13:36:58

标签: sql oracle

我需要一些帮助,将我的三个代码组合在一个匿名块中,我还需要设置一个用户将使用的替换值。任何想法我怎么能这样做?我使用的是oracle 10g express,这是我的代码:

代码1:显示“美洲”中每个部门的部门名称和每个部门的员工数量。

select department_name, count(employee_id) "number of employees"
from employees, departments, locations, countries, regions
where employees.DEPARTMENT_ID = departments.DEPARTMENT_ID 
AND departments.location_ID = locations.location_ID 
AND locations.country_ID = countries.country_ID 
AND countries.region_ID = regions.region_ID 
AND regions.region_name = 'Americas'
group by department_name

代码2:每个职位的平均工资是多少?

SELECT job_ID, AVG(salary) as "avg salary"
FROM employees
GROUP BY job_ID

代码3:每个部门的平均工资是多少?

SELECT department_ID, AVG(salary) as "avg salary"
FROM employees
GROUP BY department_ID

2 个答案:

答案 0 :(得分:1)

在Oracle中无法将完全不同的数据集组合到单个块中。匿名块用于执行一系列PL / SQL命令。查询(即返回结果集的任何内容,例如:SELECT ...)只能返回单个结果,因此需要三个单独的查询。

如果您正在使用SQL*Plus,则可以通过命令行和/或.sql脚本文件完成所有这些操作。你也可以在命令行中指定参数(我无法判断你是否对第一个查询意味着什么,但我还是添加了它。)

脚本(foo.sql):

-- Set page size to something larger than the expected result set.
-- NOTE: You don't want to put 0 (unlimited) as it supresses the column headers.
SET pagesize 50000

-- Get the parameter from the command line:
VAR region VARCHAR2(256)
EXEC :region := '&1'

SELECT 'Processing for region: ' || :region
FROM dual;

-- Query 1
promp "Employee detail in specified region:"
select department_name, count(employee_id) "number of employees"
from HR.employees, HR.departments, HR.locations, HR.countries, HR.regions
where employees.DEPARTMENT_ID = departments.DEPARTMENT_ID
AND departments.location_ID = locations.location_ID
AND locations.country_ID = countries.country_ID
AND countries.region_ID = regions.region_ID
AND regions.region_name = :region
group by department_name
;


-- Query 2:
promp "Average salary by position:"
SELECT job_ID, AVG(salary) as "avg salary"
FROM HR.employees
GROUP BY job_ID;


-- Query 3:
promp "Average salaries by department:"
SELECT department_ID, AVG(salary) as "avg salary"
FROM HR.employees
GROUP BY department_ID;

exit

输出:

$ sqlplus myuser/mypass@lvm01xe @sample.sql Americas

SQL*Plus: Release 11.2.0.2.0 Production on Sun Nov 18 09:11:11 2012

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production


PL/SQL procedure successfully completed.


'PROCESSINGFORREGION:'||:REGION
--------------------------------------------------------------------------------
Processing for region: Americas

"Employee detail in specified region:"

DEPARTMENT_NAME                number of employees
------------------------------ -------------------
Administration                                   1
Accounting                                       2
IT                                               5
Purchasing                                       6
Executive                                        3
Shipping                                        45
Finance                                          6
Marketing                                        2

8 rows selected.

"Average salary by position:"

JOB_ID     avg salary
---------- ----------
IT_PROG          5760
AC_MGR          12000
AC_ACCOUNT       8300
ST_MAN           7280
PU_MAN          11000
AD_ASST          4400
AD_VP           17000
SH_CLERK         3215
FI_ACCOUNT       7920
FI_MGR          12000
PU_CLERK         2780
SA_MAN          12200
MK_MAN          13000
PR_REP          10000
AD_PRES         24000
SA_REP           8350
MK_REP           6000
ST_CLERK         2785
HR_REP           6500

19 rows selected.

"Average salaries by department:"

DEPARTMENT_ID avg salary
------------- ----------
          100       8600
           30       4150
                    7000
           90 19333.3333
           20       9500
           70      10000
          110      10150
           50 3475.55556
           80 8955.88235
           40       6500
           60       5760
           10       4400

12 rows selected.

Disconnected from Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production

答案 1 :(得分:0)

显然,任何由相同标尺聚合的内容都可以轻松组合。因此,您可以将第三个查询与第一个查询合并,如下所示:

select department_name
       , count(employee_id) as "number of employees"
       , avg(salary) as "avg salary"
from employees, departments, locations, countries, regions
where employees.DEPARTMENT_ID = departments.DEPARTMENT_ID 
AND departments.location_ID = locations.location_ID 
AND locations.country_ID = countries.country_ID 
AND countries.region_ID = regions.region_ID 
AND regions.region_name = :region_name
group by department_name

我已调整您的代码以传入绑定变量:region_name。在Apex中有多种指定变量的方法。 Find out more

包含第二个查询没有任何有意义的方法。 JOB_ID的值越过DEPARTMENT_ID的值,因此没有要加入的公共密钥。