와 SQL에 다른 데이터에서 데이터를 찾을 수 :내가 테이블이 같은 얼마나 커서
create table police(
ssn
name
boss_name
salary
);
Insert into police values(1,’A’,’C’,10000);
Insert into police values(2,’B’,’D’,20000);
Insert into police values(3,’C’,’E’,30000);
Insert into police values(4,’D’,’E’,45000);
Insert into police values(5,’E’,NULL,55000);
나는 polices who have same boss
에 대한 total salary
을 반환하는 cursor
를 원한다. 결과는 다음과 같습니다. 30000(salary of C)+45000(salary of D)
둘 다 보스 아래에 있습니다. 어떻게해야합니까? 내가 C와 D를 ORACLE pl/sql 또는 익명 블록에서 E 아래에있는 것처럼 찾는 방법은 무엇입니까?
set serveroutput on;
declare
cboss_name varchar2(30) ;
csalary number ;
CURSOR cur
is
select boss_name,sum(salary) as salary
from police
where boss_name is not null
group by boss_name;
begin
open cur;
fetch cur into cboss_name,csalary;
dbms_output.put_line('boss_name'||' '||'csalary');
LOOP
FETCH cur INTO cboss_name,csalary;
EXIT WHEN cur%NOTFOUND;
dbms_output.put_line(cboss_name||' '||csalary);
END LOOP;
close cur;
end;
Here 당신이 커서 및 루프에 대한 자세한 정보를 얻을 수 있습니다
그게 기본 SQL은, 당신은 그룹을 사용할 수 있습니다. 귀하의 커서 쿼리는 '보스 이름으로 경찰 그룹에서 총액 (급여)를 선택 boss_name'처럼 될 것입니다. –
Reg : "ORACLE pl/sql 또는 익명 블록에서 C 및 D를 찾는 방법".. 이것은 보고서와 같습니다 .. 원하는 형식에 따라 ... 찾고있는 출력은 무엇입니까? 에 대한? –
@pratikgarg : 분석 함수를 사용할 수 있습니다. 'sum (salary) over partition (boss_name by ssn by order) total_salary' –