英文:
PL/SQL - If statement to check salary
问题
我正在尝试开发一个程序,该程序从用户那里获取一个数字,按照最高工资对员工进行排序,并显示与输入数字相等的行数,显示收入最高的员工。程序还应验证最后一个员工是否获得与下一个员工相同的工资。如果是这种情况,下一个员工也应该被显示。
我已经尝试修改了代码,但似乎我的更改都不起作用。您对如何使代码符合提供的描述操作有任何建议吗?以下是功能性代码。
谢谢。
CREATE OR REPLACE PROCEDURE p5_q5 (num_employee NUMBER) AS
BEGIN
FOR emp IN (SELECT ENAME, SAL
FROM EMP
ORDER BY SAL DESC
FETCH FIRST num_employee ROWS ONLY) -- 根据用户输入的num_employee返回X行
LOOP
DBMS_OUTPUT.PUT_LINE('员工姓名: ' || emp.ENAME || ' - 工资: ' || emp.SAL);
END LOOP;
END;
/
EXEC p5_q5(3);
*输出:
员工姓名: KING - 工资: 5512.5
员工姓名: JONES - 工资: 3570
员工姓名: SCOTT - 工资: 3450
*理想输出:
员工姓名: KING - 工资: 5512.5
员工姓名: JONES - 工资: 3570
员工姓名: SCOTT - 工资: 3450
员工姓名: FORD - 工资: 3450
我尝试了几种策略,但未能成功开发一种方法来确定后续员工是否获得与前一个员工相同的工资,并在他们获得相同金额时显示该员工。
英文:
I'm attempting to develop a program that takes a number from the user, sorts employees by their highest salary, and displays a number of rows equivalent to the input number showing the top earners. The program should also verify if the last employee received the same salary as the next employee. If this is the case, the next employee should also be displayed.
I have attempted to modify the code, but none of my changes seem to be effective. Do you have any suggestions for how to make the code operate in accordance with the description provided? The functional code is included below.
Thank you.
Code:
CREATE OR REPLACE PROCEDURE p5_q5 (num_employee NUMBER) AS
BEGIN
FOR emp IN (SELECT ENAME, SAL
FROM EMP
ORDER BY SAL DESC
FETCH FIRST num_employee ROWS ONLY) -- Will be returned X rows according to input of the user (num_employee)
LOOP
DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp.ENAME || ' - Salary: ' || emp.SAL);
END LOOP;
END;
/
EXEC p5_q5(3);
*Output:
Employee name: KING - Salary: 5512.5
Employee name: JONES - Salary: 3570
Employee name: SCOTT - Salary: 3450
*Ideal Output:
Employee name: KING - Salary: 5512.5
Employee name: JONES - Salary: 3570
Employee name: SCOTT - Salary: 3450
Employee name: FORD - Salary: 3450
I have tried several strategies, but have been unsuccessful in developing a method to determine if the subsequent employee earns the same salary as the previous employee, and to display that employee if they do earn the same amount.
答案1
得分: 2
使用FETCH FIRST n ROWS WITH TIES
:
CREATE OR REPLACE PROCEDURE p5_q5 (num_employee NUMBER) AS
BEGIN
FOR emp IN (SELECT ENAME, SAL
FROM EMP
ORDER BY SAL DESC
FETCH FIRST num_employee ROWS WITH TIES)
LOOP
DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp.ENAME || ' - Salary: ' || emp.SAL);
END LOOP;
END;
/
对于示例数据:
CREATE TABLE emp (ename, sal) AS
SELECT 'Alice', 100 FROM DUAL UNION ALL
SELECT 'Betty', 100 FROM DUAL UNION ALL
SELECT 'Carol', 90 FROM DUAL UNION ALL
SELECT 'Debra', 90 FROM DUAL UNION ALL
SELECT 'Emily', 90 FROM DUAL;
然后:
BEGIN
DBMS_OUTPUT.ENABLE;
p5_q5(1);
END;
/
和
BEGIN
DBMS_OUTPUT.ENABLE;
p5_q5(2);
END;
/
都输出:
Employee name: Alice - Salary: 100 Employee name: Betty - Salary: 100
和:
BEGIN
DBMS_OUTPUT.ENABLE;
p5_q5(3);
END;
/
输出:
Employee name: Alice - Salary: 100 Employee name: Betty - Salary: 100 Employee name: Carol - Salary: 90 Employee name: Debra - Salary: 90 Employee name: Emily - Salary: 90
英文:
Use FETCH FIRST n ROWS WITH TIES
:
CREATE OR REPLACE PROCEDURE p5_q5 (num_employee NUMBER) AS
BEGIN
FOR emp IN (SELECT ENAME, SAL
FROM EMP
ORDER BY SAL DESC
FETCH FIRST num_employee ROWS WITH TIES)
LOOP
DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp.ENAME || ' - Salary: ' || emp.SAL);
END LOOP;
END;
/
Which, for the sample data:
CREATE TABLE emp (ename, sal) AS
SELECT 'Alice', 100 FROM DUAL UNION ALL
SELECT 'Betty', 100 FROM DUAL UNION ALL
SELECT 'Carol', 90 FROM DUAL UNION ALL
SELECT 'Debra', 90 FROM DUAL UNION ALL
SELECT 'Emily', 90 FROM DUAL;
Then:
BEGIN
DBMS_OUTPUT.ENABLE;
p5_q5(1);
END;
/
and
BEGIN
DBMS_OUTPUT.ENABLE;
p5_q5(2);
END;
/
Both output:
> lang-none
> Employee name: Alice - Salary: 100
> Employee name: Betty - Salary: 100
>
and:
BEGIN
DBMS_OUTPUT.ENABLE;
p5_q5(3);
END;
/
Outputs:
> lang-none
> Employee name: Alice - Salary: 100
> Employee name: Betty - Salary: 100
> Employee name: Carol - Salary: 90
> Employee name: Debra - Salary: 90
> Employee name: Emily - Salary: 90
>
答案2
得分: 0
这是您提供的代码,我不会为您执行代码,但我可以帮您理解其功能。以下是代码的翻译:
创建或替换过程 p5_q5 (num_employee NUMBER) 作为
curr_salary NUMBER;
开始
curr_salary := 0;
对于 emp 在 (选择 ENAME,SAL
从 EMP
按 SAL 降序排序)
循环
如果 emp.SAL = curr_salary THEN
DBMS_OUTPUT.PUT_LINE('员工姓名: ' || emp.ENAME || ' - 薪水: ' || emp.SAL);
num_employee := num_employee - 1;
否则
curr_salary := emp.SAL;
DBMS_OUTPUT.PUT_LINE('员工姓名: ' || emp.ENAME || ' - 薪水: ' || emp.SAL);
num_employee := num_employee - 1;
结束 IF;
如果 num_employee = 0 THEN
退出;
结束 IF;
结束 循环;
结束;
/
执行 p5_q5(3);
请注意,这段代码似乎是一个PL/SQL存储过程,用于从EMP表中选择薪水最高的员工,并按薪水降序排序。然后,它会逐个输出员工的姓名和薪水,直到输出指定数量的员工(在这里是3个员工)或直到没有更多的符合条件的员工为止。
英文:
CREATE OR REPLACE PROCEDURE p5_q5 (num_employee NUMBER) AS
curr_salary NUMBER;
BEGIN
curr_salary := 0;
FOR emp IN (SELECT ENAME, SAL
FROM EMP
ORDER BY SAL DESC)
LOOP
IF emp.SAL = curr_salary THEN
DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp.ENAME || ' - Salary: ' || emp.SAL);
num_employee := num_employee - 1;
ELSE
curr_salary := emp.SAL;
DBMS_OUTPUT.PUT_LINE('Employee name: ' || emp.ENAME || ' - Salary: ' || emp.SAL);
num_employee := num_employee - 1;
END IF;
IF num_employee = 0 THEN
EXIT;
END IF;
END LOOP;
END;
/
EXEC p5_q5(3);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论