sourcecode

Oracle PL/SQL에서 테이블 루프

codebag 2023. 7. 22. 09:59
반응형

Oracle PL/SQL에서 테이블 루프

SQL 쿼리는 해봤지만 루프를 이용한 프로시저 작성은 해본 적이 없어 막막합니다.Oracle SQL Developer를 사용하고 있습니다.SQL 또는 PL/SQL에서 수행 가능

다음과 같은 테이블이 있습니다.

Person_ID Score Name  Game_ID
1         10    jack  1
1         20    jack  2
2         15    carl  1
2         3     carl  3
4         17    steve 1

이 표를 순환하여 모든 게임의 총 점수를 얻으려면 어떻게 해야 합니까?결과는 다음과 같습니다.

Person_ID Score Name
1         30    jack
2         18    carl
4         17    steve

또한 추가 크레딧은 제가 게임 12를 잡고 싶다면 무엇입니까?


편집: 명확하게 하지 못해서 미안하지만, 나는 그것 없이도 할 수 있지만 루프로 이것을 해야 합니다.

사후 편집 후 솔루션

이 절차에서는 지정된 game_id에 대한 점수를 나열합니다.매개 변수를 생략하면 모든 게임이 합계됩니다.

create or replace procedure player_scores(i_game_id number default null) as 
begin
  for o in (select person_id, name, sum(score) score
      from games where game_id = nvl(i_game_id, game_id)
      group by person_id, name)
  loop
    dbms_output.put_line(o.person_id||' '||o.name||' '||o.score);
  end loop;
end player_scores;

이전 솔루션:

이를 위한 절차는 필요하지 않고 간단한 질문만 하면 됩니다.

select person_id, name, sum(score)
  from your_table
  where game_id in (1, 2)
  group by person_id, name

언급URL : https://stackoverflow.com/questions/28387339/loop-through-a-table-in-oracle-pl-sql

반응형