0

私は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 ブロック ロジックの小さな変更であることは理解していますが、理解できません。

4

1 に答える 1

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;

分析関数 を使用して、COUNT() OVER ()部門ごとに結果の従業員数をカウントし、その部門のすべての行にそれを挿入します。

クエリはすでに部門ごとに並べ替えられているため、別の値に変更された時点を確認してから、ヘッダーを出力します。

于 2012-07-31T02:44:01.927 に答える