创建部门表
CREATE TABLE department
( DEPARTMENT_ID NUMBER(4) PRIMARY KEY,
DEPARTMENT_NAME VARCHAR2(20) NOT NULL UNIQUE,
ADDRESS VARCHAR2(20) NOT NULL);
填充部门表
INSERT INTO department VALUES(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO department VALUES(20, 'RESEARCH', 'DALLAS');
INSERT INTO department VALUES(30, 'SALES', 'CHICAGO');
INSERT INTO department VALUES(40, 'IT', 'DALLAS');
INSERT INTO department VALUES(50, 'EXECUTIVE', 'NEW YORK');
INSERT INTO department VALUES(60, 'MARKETING', 'CHICAGO');
COMMIT;
创建员工表
CREATE TABLE employee
( EMPLOYEE_ID NUMBER(4) PRIMARY KEY,
EMPLOYEE_NAME VARCHAR2(20) NOT NULL,
JOB VARCHAR2(50) NOT NULL,
MANAGER_ID NUMBER(4),
HIRE_DATE DATE NOT NULL,
SALARY NUMBER(9, 2) NOT NULL,
COMMISSION NUMBER(9, 2),
DEPARTMENT_ID NUMBER(4) REFERENCES department(DEPARTMENT_ID));
填充员工表
INSERT INTO employee
VALUES(7839, 'KING', 'PRESIDENT', NULL, '20-NOV-01', 5000, NULL, 50);
INSERT INTO employee
VALUES(7596, 'JOST', 'VICE PRESIDENT', 7839, '04-MAY-01', 4500, NULL, 50);
INSERT INTO employee
VALUES(7603, 'CLARK', 'VICE PRESIDENT', 7839, '12-JUN-01', 4000, NULL, 50);
INSERT INTO employee
VALUES(7566, 'JONES', 'PUBLIC ACCOUNTANT', 7596, '05-APR-01', 3000, NULL, 10);
INSERT INTO employee
VALUES(7886, 'STEEL', 'PUBLIC ACCOUNTANT', 7566, '08-MAR-03', 2500, NULL, 10);
INSERT INTO employee
VALUES(7610, 'WILSON', 'ANALYST', 7596, '03-DEC-01', 3000, NULL, 20);
INSERT INTO employee
VALUES(7999, 'WOLFE', 'ANALYST', 7610, '15-FEB-02', 2500, NULL, 20);
INSERT INTO employee
VALUES(7944, 'LEE', 'ANALYST', 7610, '04-SEP-06', 2400, NULL, 20);
INSERT INTO employee
VALUES(7900, 'FISHER', 'SALESMAN', 7603, '06-DEC-01', 3000, 500, 30);
INSERT INTO employee
VALUES(7921, 'JACKSON', 'SALESMAN', 7900, '25-FEB-05', 2500, 400, 30);
INSERT INTO employee
VALUES(7952, 'LANCASTER', 'SALESMAN', 7900, '06-DEC-06', 2000, 150, 30);
INSERT INTO employee
VALUES(7910, 'SMITH', 'DATABASE ADMINISTRATOR', 7596, '20-DEC-01', 2900, NULL, 40);
INSERT INTO employee
VALUES(7788, 'SCOTT', 'PROGRAMMER', 7910, '15-JAN-03', 2500, NULL, 40);
INSERT INTO employee
VALUES(7876, 'ADAMS', 'PROGRAMMER', 7910, '15-JAN-03', 2000, NULL, 40);
INSERT INTO employee
VALUES(7934, 'MILLER','PROGRAMMER', 7876, '25-JAN-02', 1000, NULL, 40);
INSERT INTO employee
VALUES(8000, 'BREWSTER', 'TBA', NULL, '22-AUG-13', 2500, NULL, NULL);
COMMIT;
问题:根据在编写PL / SQL匿名块时创建的DEPARTMENT和EMPLOYEE表,该块接受来自用户输入的员工ID并显示1)员工的姓名,雇用日期和总数支付(工资+佣金),2)他/她的经理姓名,雇用日期和总工资(工资+佣金),3)雇员工作的部门的名称,4)部门中的雇员人数员工工作,以及5)员工工作部门的总薪酬(工资+佣金)总和
到目前为止,我已经得到了以下查询,但我无法获得在该部门工作的员工人数。我怎么能这样做?
ACCEPT p_1 PROMPT 'Please enter the employee id:'
DECLARE
v_input_emp_id NUMBER := &p_1;
v_emp_name employee.employee_name%TYPE;
v_dept_name department.department_name%TYPE;
v_pay employee.salary%TYPE;
v_n NUMBER;
v_output VARCHAR2(100) := 'The employee id is not in the table';
v_hiredate EMPLOYEE.HIRE_DATE%TYPE;
BEGIN
SELECT COUNT(*)
INTO v_n
FROM employee
WHERE employee_id = v_input_emp_id;
IF v_n = 0 THEN
DBMS_OUTPUT.PUT_LINE(v_input_emp_id || ' is not in the EMPLOYEE table.');
END IF;
IF v_n = 1 THEN
SELECT employee_name, salary + NVL(commission, 0),HIRE_DATE
INTO v_emp_name, v_pay, v_hiredate
FROM employee
WHERE employee_id = v_input_emp_id;
DBMS_OUTPUT.PUT_LINE('USER INPUT:');
DBMS_OUTPUT.PUT_LINE(v_input_emp_id);
DBMS_OUTPUT.PUT_LINE('----------EMPLOYEE----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
DBMS_OUTPUT.PUT_LINE(rpad('HIRE DATE:',10) || lpad(v_hiredate,20));
DBMS_OUTPUT.PUT_LINE('TOTAL PAY:' || lpad(TO_CHAR(ROUND(v_pay, 2),
'$9,990.00'),21));
IF v_input_emp_id IS NOT NULL THEN
SELECT manager_id
INTO v_MANAGER_ID
FROM EMPLOYEE
WHERE employee_id = v_input_emp_id;
SELECT employee_name, salary + NVL(commission, 0),HIRE_DATE
INTO v_emp_name, v_pay, v_hiredate
FROM employee
WHERE v_manager_id = employee_id;
DBMS_OUTPUT.PUT_LINE('----------MANAGER----------');
DBMS_OUTPUT.PUT_LINE(rpad('NAME:',10,' ') || lpad(v_emp_name,15));
DBMS_OUTPUT.PUT_LINE(rpad('HIRE DATE:',10) || lpad(v_hiredate,20));
DBMS_OUTPUT.PUT_LINE('TOTAL PAY:' || lpad(TO_CHAR(ROUND(v_pay, 2),
'$9,990.00'),21));
ELSE
DBMS_OUTPUT.PUT_LINE ('NO DATA');
END IF;
IF v_n = 1 THEN
SELECT department_name
INTO v_dept_name
FROM department
INNER JOIN employee
ON department.department_id = employee.department_id
WHERE employee_id = v_input_emp_id;
/*
SELECT department_id
INTO v_dept_id
from department
WHERE department_id = v_dept_id;
SELECT COUNT(*)
INTO v_emp_num
FROM employee INNER JOIN DEPARTMENT
ON DEPARTMENT.DEPARTMENT_ID = EMPLOYEE.DEPARTMENT_ID
WHERE employee.department_id = department.v_dept_id;
*/
DBMS_OUTPUT.PUT_LINE('----------DEPARTMENT----------');
DBMS_OUTPUT.PUT_LINE('NAME: ' || v_dept_name);
/*DBMS_OUTPUT.PUT_LINE('NUMBER OF EMPLOYEES' || ' ' || TO_CHAR(v_emp_num));
*/
ELSE
DBMS_OUTPUT.PUT_LINE('No output!');
END IF;
END IF;
END;
答案 0 :(得分:0)
您可能需要对简历进行简单查询,例如:
SELECT department_name
, count(employee_id) over (partition by department_id) AS count_of_employee
, EMPLOYEE_NAME
, HIRE_DATE
, SALARY + COMMISSION AS pay
, managers.EMPLOYEE_NAME AS manager
, sum(SALARY + COMMISSION) over (partition by department_id) AS total_pay
FROM department
INNER JOIN employee ON department.department_id = employee.department_id
LEFT JOIN employee as managers ON managers.employee_id = employee.MANAGER_ID
WHERE department_id = .........;
或员工:
SELECT department_name
, count_of_employee
, EMPLOYEE_NAME
, HIRE_DATE
, SALARY + COMMISSION AS pay
, managers.EMPLOYEE_NAME AS manager
, total_pay
FROM employee
INNER JOIN department ON department.department_id = employee.department_id
INNER JOIN (
SELECT Count(1) AS count_of_employee
, Sum(SALARY + COMMISSION) AS total_pay
, department_id
FROM employee
GROUP BY department_id
) AS employee_total ON employee_total.department_id = employee.department_id
LEFT JOIN employee as managers ON managers.employee_id = employee.MANAGER_ID
WHERE employee_id = v_input_emp_id;