그룹

2013-08-05 5 views
0

enter image description here그룹

C1 내림차순 포인트 및 CompetitionName하여 각 섹션 - Competition1, C2 - 설정해 경쟁, C3 - Competition3. C1 점 - Compeletion1 점

이 표는 다양한 유형의 대회에 참가하는 학생들의 다양한 범주가있는 점수 표를 나타냅니다.

열 C1, C2, C3은 해당 경기에 참여한 값이 '1'이면 대회 이름입니다. '0'이면 참여하지 않습니다.

S1 to S15      : Student Names 
C1 ,C2,C3      : Competition Names 
Section      : Indicates category of that particular student 
C1points, C2points, C3points : Points received by the particular student in that particular competition 

각 섹션을 점수 및 경쟁 이름 내림차순으로 그룹화하고 싶습니다.

참고 : Competition1, C2 변경해야 C1 - 설정해 경쟁, C3 - Competition3

를 참조하십시오 : http://www.sqlfiddle.com/#!2/20920/1

내 최종 출력 만

같은

enter image description here

+0

참가하는 대회에서 0 점을 획득 할 수 있습니까? – Strawberry

+0

예 ... 문제 없습니다 .. 도와주세요 – user2594154

+0

ID가 13 인 경우 섹션 1의 – kevinm

답변

1

당신은 결합 된 모든 섹션에 대한 쿼리를 다음과 같이 시도 할 수 있습니다 : 난 아직도 생각하지만

select ID,name,comp_desc,points 
from (
select section,ID,name, 'Competion1' as comp_desc, C1Points as points 
from students 
union 
select section,ID,name, 'Competion2' as comp_desc, C2Points as points 
from students 
union 
select section,ID,name, 'Competion3' as comp_desc, C3Points as points 
from students 
) t 
where points <> 0 
group by section, name,comp_desc, points 
order by section, points desc, comp_desc desc; 

출력이 보여 당신이 조금 더 많은 설명을 필요로. 예를 들어, Luv에 의해 식별 된 이유는 S13 경쟁 2가 3 이전에 오는 이유입니다. 2 절에서도 S14가 경쟁 S10보다 먼저 나오는 이유는 무엇입니까?

그러나 이제 위 쿼리를 다음과 같이 수정할 수 있다고 생각합니다. 귀하의 필요에 부응하십시오.

+0

집계 함수가 없으면 GROUP BY를 사용하는 것은 적절하지 않습니다. 분명히이 쿼리 어딘가에 집계 함수를 사용해야합니다 (그러나 점으로 그룹화하지 마십시오!). 그것은 결국, 그것의 요점입니다! – Strawberry

+0

뛰어난 쿼리 .. 정말 고맙습니다. – user2594154

0
select * from 
(select 
    section 
    ,id 
    ,name 
    ,"Competition 1" as Competition 
    ,C1Points as Points 
from 
    students 
where 
    C1 = 1 
union 
select 
    section 
    ,id 
    ,name 
    ,"Competition 2" as Competition 
    ,C2Points as Points 
from 
    students 
where 
    C2 = 1 
union 
select 
    section 
    ,id 
    ,name 
    ,"Competition 2" as Competition 
    ,C2Points as Points 
from 
    students 
where 
    C3 = 1 
) agg 
order by 
section, points desc 
+0

안녕하세요,이 코드는 SQL 바이올린에서 작동하지 않습니다. 제발 당신을 확인하시기 바랍니다 http://www.sqlfiddle.com/#!2/20920/12 – user2594154

+0

지금 시도, 고정해야 –

+0

예 .. 작동 좋아요 ... 훌륭한 일을 내 친구 .. 정말 고마워요 – user2594154

1

보인다 Section 2와 Section 3에 대한 로직을 구현할 수 있습니다.

select * 
from 
(
select s1.ID, 
s1.Name, 
'Competition 1' as Competition, 
s1.C1Points as Points 
from students s1 
where SECTION='Section1' 
and ifnull(s1.C1,'')<>'' 
and s1.C1Points<>0 
union 
select s2.ID, 
s2.Name, 
'Competition 2' as Competition, 
s2.C2Points as Points 
from students s2 
where s2.SECTION='Section1' 
and ifnull(s2.C2,'')<>'' 
and s2.C2Points<>0 
union 
select s3.ID, 
s3.Name, 
'Competition 3' as Competition, 
s3.C3Points as Points 
from students s3 
where s3.SECTION='Section1' 
and ifnull(s3.C2,'')<>'' 
and s3.C3Points<>0 
)t 
order by t.points desc,t.Competition desc 

SQl Fiddle

+0

고마워요 너무 많이 – user2594154