2011-01-04 2 views
1

내가 다음 쿼리를 얻을 수있는 어려움에 직면하고있어 쿼리 많은 플레이어 테이블). 여러 상위 N은

그 선수

14 0에서하는 주문 칼럼을 통해, 팀 내에서 주문하고 두 가지 속성이있다 : 값 0

  • 점 득점 (정수)
  • wasSick (부울 또는 정수를 내가 첫 번째 열은 팀 ID (기본 키) 및 두 번째 열의 각 팀, 대한 행을 반환하는 쿼리를하고 싶습니다

) 사실에 대한 허위 또는 1 점의 합계입니다 처음 11 pl에서 득점 Order 열에 정의 된 순서대로 아프지 않은 각 팀의 팀원.

팀에 3 명이 넘는 선수가 병에 걸린 경우 나머지 팀을 모두 사용하게됩니다.

이것은 오라클에 대한 것입니다. 내가 ROWNUM을 사용하여 노력했지만 각 집계에 대해 재설정 비슷한 rownum 필요합니다. 쿼리는 하위 쿼리를 포함하거나 WITH 절을 사용할 수 있습니다.

감사합니다.

PD : 추측하면 축구/축구 팀입니다.

편집

팀에서 선수의 순서를 알고하는 데 사용되는 속성 주문 열입니다. Position 열이라고 부르 자고, 0에서 14까지의 정수를가집니다. 그래서 팀에 있다면, 선수 2와 4는 아플 것입니다. 선수 0,1,3,5,6에 의해 만들어진 점수의 합이 필요합니다. 7,8,9,10,11 및 12

내 시도였다 같은 :

SELECT t.id, sum(p.points) FROM team t, points p WHERE p.t_id = t.id AND p.wasSick = 0 AND ROWNUM < 12 GROUP BY p.t_id 

여기에서 잘못된 것은 내가 p.position에 의해 ORDER를 누락하고 ROWNUM가 재설정되지 않는다는 것입니다 팀 간.

+0

당신이 말하는 "각 팀의 첫 번째 11 플레이어에서 득점 포인트는"모든 속성이 /가 주문에 사용할 수있는 열? – Chandu

+1

시도를 게시 할 수 있습니까? –

답변

3

처음 11 명의 플레이어를 식별하는 데 사용할 수있는 필드에 대한 참조가 없으므로 (팀원이 아프지 않은 경우) 나는 그들을 정렬하기 위해 ROWID를 사용했습니다. 이러한 필드로 질문을 업데이트 할 수 있으면 쿼리를 변경할 수 있습니다. 그때까지 :

SELECT team_id, 
     SUM(points) 
    FROM (
     SELECT t.team_id, 
       p.points 
       ROW_NUMBER() OVER(PARTITION BY team_id ORDER BY player_id, p.rowid) rn 
      FROM teams t, players p 
       WHERE t.team_id = p.team_id 
       AND p.wasSick = 0 
     ) 
WHERE rn < 12 
GROUP BY team_id 
+0

솔루션을 검색하는 동안 ROW_NUMBER을 (를) 찾았지만 이해하지 못했습니다. 그것은 당신의 모범을 보게되었습니다. 나는 내일 다시 일할 때이 접근법을 시도 할 것입니다. – Luciano

+0

예, row_number는 내가 찾고있는 단어입니다. – Luciano

0

이 방법에 대해 : 리턴

select teamid, 
     sum(pointscored) 
    from (select teamid, 
       pointscored, 
       row_number() over (partition by teamid order by orderid) rn 
      from (select 1 teamid, 1 playerid, 0 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 2 playerid, 1 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 3 playerid, 2 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 4 playerid, 3 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 5 playerid, 4 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 6 playerid, 5 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 7 playerid, 6 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 8 playerid, 7 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 9 playerid, 8 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 10 playerid, 9 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 11 playerid, 10 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 12 playerid, 11 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 13 playerid, 12 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 14 playerid, 13 orderid, 1 pointscored, 0 wassick from dual union all 
       select 1 teamid, 15 playerid, 14 orderid, 1 pointscored, 0 wassick from dual union all 
       select 2 teamid, 1 playerid, 0 orderid, 1 pointscored, 1 wassick from dual union all 
       select 2 teamid, 2 playerid, 1 orderid, 1 pointscored, 1 wassick from dual union all 
       select 2 teamid, 3 playerid, 2 orderid, 1 pointscored, 1 wassick from dual union all 
       select 2 teamid, 4 playerid, 3 orderid, 1 pointscored, 1 wassick from dual union all 
       select 2 teamid, 5 playerid, 4 orderid, 1 pointscored, 0 wassick from dual union all 
       select 2 teamid, 6 playerid, 5 orderid, 1 pointscored, 0 wassick from dual union all 
       select 2 teamid, 7 playerid, 6 orderid, 1 pointscored, 0 wassick from dual union all 
       select 2 teamid, 8 playerid, 7 orderid, 1 pointscored, 0 wassick from dual union all 
       select 2 teamid, 9 playerid, 8 orderid, 1 pointscored, 0 wassick from dual union all 
       select 2 teamid, 10 playerid, 9 orderid, 1 pointscored, 0 wassick from dual union all 
       select 2 teamid, 11 playerid, 10 orderid, 1 pointscored, 0 wassick from dual union all 
       select 2 teamid, 12 playerid, 11 orderid, 1 pointscored, 0 wassick from dual union all 
       select 2 teamid, 13 playerid, 12 orderid, 1 pointscored, 0 wassick from dual union all 
       select 2 teamid, 14 playerid, 13 orderid, 1 pointscored, 0 wassick from dual union all 
       select 2 teamid, 15 playerid, 14 orderid, 1 pointscored, 0 wassick from dual 
       ) teams 
     where wassick = 0 
     ) 
where rn <= 11 
group by teamid; 

:

TEAMID SUM(POINTSCORED) 
---------- ---------------- 
     1    11 
     2    11 

2 rows selected. 
+0

은 @Cybernate와 거의 같지만 windowing 절에서 orderid로 정렬합니다. 큰 마음은 다 비슷 하네. –

+0

좋아요, 제 문제가 무엇인지 이해 했으니 이제는 당신의 해결책을 이해해야합니다! 쿼리 내에서 쿼리 내에서 쿼리가 깊게 들립니다. 필요한 쿼리가 이미 더 큰 쿼리 내부로 들어갈 것입니다. – Luciano

+0

@Luciano : 중첩 된 쿼리는 두려워 할 필요가 없습니다. 대부분의 쿼리는 샘플 데이터입니다. 그것이하는 일은 다음과 같습니다. 1) 아픈 사람 제외 2) 각 팀의 분석 함수 (row_number)를 사용하여 orderid에 의해 주문 된 해당 팀의 각 행에 오름차순 번호 부여 3) 1에서 11까지의 숫자를 제외한 모든 것을 킥오프합니다. 우리의 분석 함수의 결과 4) 점수를 요약하는 팀별 그룹화. –

관련 문제