我是PL / SQL的新手,我陷入了这一点,我无法按部门名称对数据进行分组。我的查询如下
DECLARE
CURSOR dept_cur IS
select department_name , NVL(employee_name , 'N/A') , NVL(employee.JOB , 'N/A') , NVL(to_char(hire_date, 'DD-MON-YYYY') , 'N/A')
FROM department FULL OUTER JOIN employee ON department.department_id = employee.department_id
order by department_name , employee_name;
v_department_name department.department_name%TYPE;
v_employee_name employee.employee_name%type;
v_employee_job employee.job%type;
v_hire_date VARCHAR(20);
BEGIN
OPEN dept_cur;
fetch dept_cur into v_department_name , v_employee_name , v_employee_job , v_hire_date ;
IF dept_cur%FOUND THEN
DBMS_OUTPUT.PUT_LINE('DEPARTMENT_NAME EMPLOYEE NAME , EMPLOYEE JOB , HIRE DATE');
DBMS_OUTPUT.PUT_LINE('-----------------------------------------------------');
WHILE dept_cur%FOUND LOOP
DBMS_OUTPUT.PUT_LINE(lpad(v_department_name, 11) || lpad(v_employee_name, 16) || lpad(v_employee_job, 22)|| lpad(v_hire_date,15));
fetch dept_cur into v_department_name , v_employee_name , v_employee_job , v_hire_date ;
END LOOP;
ELSE
DBMS_OUTPUT.PUT_LINE('No output.');
END IF;
CLOSE dept_cur;
END;
我希望输出类似于此,但我的查询返回整个列表。
ACCOUNTING的员工人数为2
EMPLOYEE_NAME JOB HIRE DATE
--------------------------------------------------------------
JONES PUBLIC ACCOUNTANT 02-APR-81
STEEL PUBLIC ACCOUNTANT 02-MAR-83
和其他部门一样。我理解它是PL SQL块逻辑的一个小改动,但我无法弄明白。
答案 0 :(得分:0)
DECLARE
CURSOR dept_cur IS
SELECT department_name,
COUNT(employee.employee_id) OVER (PARTITION BY department_name),
NVL(employee_name, 'N/A'),
NVL(employee.JOB, 'N/A'),
NVL(to_char(hire_date, 'DD-MON-YYYY'), 'N/A')
FROM department
FULL OUTER JOIN employee ON department.department_id = employee.department_id
ORDER BY department_name, employee_name;
v_department_name department.department_name%TYPE;
last_department_name department.department_name%TYPE;
v_department_count NUMBER;
v_employee_name employee.employee_name%TYPE;
v_employee_job employee.job%TYPE;
v_hire_date VARCHAR(20);
BEGIN
OPEN dept_cur;
FETCH dept_cur INTO v_department_name, v_department_count, v_employee_name, v_employee_job, v_hire_date;
last_department_name := NULL;
IF dept_cur%FOUND THEN
WHILE dept_cur%FOUND LOOP
IF last_department_name IS NULL OR last_department_name <> v_department_name THEN
DBMS_OUTPUT.PUT_LINE('The number of employees in ' || v_department_name || ' is ' || to_char(v_department_count));
DBMS_OUTPUT.PUT_LINE('EMPLOYEE NAME EMPLOYEE JOB HIRE DATE');
DBMS_OUTPUT.PUT_LINE('--------------------------------------------------------------');
last_department_name := v_department_name;
END IF;
DBMS_OUTPUT.PUT_LINE(rpad(v_employee_name, 24) || rpad(v_employee_job, 24)|| lpad(v_hire_date,14));
FETCH dept_cur INTO v_department_name, v_department_count, v_employee_name, v_employee_job, v_hire_date;
END LOOP;
ELSE
DBMS_OUTPUT.PUT_LINE('No output.');
END IF;
CLOSE dept_cur;
END;
它使用analytic function COUNT() OVER ()
来计算每个部门的结果中的员工数量,并将其插入该部门的每一行。
由于查询已按部门排序,因此只会检查何时更改为其他值,然后打印标题。