2013-07-13 1 views
0

필자는 파생 된 피벗 테이블을 구성하는 상황에서 피벗 피벗 테이블을 만드는 다양한 기준에 따라 subselect를 수행 할 수 있기를 바랍니다. 그것은 다음과 같이 보일 것이다 의사에파생 테이블에서 subselect를 수행 할 수있는 방법이 있습니까?

그래서 ...

이 난 후 무엇을 설명하기 위해 터무니없이 간단한 예이라고
select 
    (select sum(score) from derivedTable where username like 'a%') as scoresForANames, 
    (select sum(score) from derivedTable where username like 'b%') as scoresForBNames 

from 
    (select username, score from gameTable where gameId = 111) derivedTable 

참고 ...이 필요하지 않습니다 예제가 전혀 해결되지 않았습니다 ... 동일한 결과를 얻으려면 mySQL에 개념적 방법이 있는지 이해해야합니다.

derivedTable은 외부 선택에서 subselects에 표시되지 않습니다. 그래서, 나는 같은 결과를 얻을 수있는 방법이 궁금하거나, 모든 기준을 개별적으로 고려한 subselect를 작성해야 할 때가있다.

답변

1

당신이 문구 원하는 방식으로 쿼리에 더 하위 쿼리가없는 것입니다 모든 select 절 :

물론
select sum(case when username like 'a%' then score end) as scoresForANames, 
     sum(case when username like 'b%' then score end) as scoresForBNames 
from (select username, score 
     from gameTable 
     where gameId = 111 
    ) derivedTable; 

는,이 경우에, 당신은 전혀 파생 테이블을 필요로하지 않습니다

select sum(case when username like 'a%' then score end) as scoresForANames, 
     sum(case when username like 'b%' then score end) as scoresForBNames 
from gameTable gt 
where gameId = 111; 
+0

ah HA! 열쇠는 subselect없이 case 문을 사용할 수있게되었습니다! 그게 정확히 내가 분명히 밝히지 않은 것이고 내 문제의 핵심입니다. 합계 이외의 case 문을 사용할 수 있습니까? 아니면 어떤 방법으로 포장해야합니까? (나는 언제나 무해한 concat이나 메소드 래퍼가 필요하다면 사용할 수있다). –