0

在此处输入图像描述

C1 - 比赛 1,C2 - 比赛 2,C3 - 比赛 3。C1Points - Completion1Points

该表代表一个积分表,其中不同类别的学生参加不同类型的比赛。

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

在这里,我想按分数和比赛名称降序对每个部分进行分组。

请注意:C1 需要更改 Competition1, C2 - Competition2, C3 - Competition3

请参考:http ://www.sqlfiddle.com/#!2/20920/1

我的最终输出看起来像

在此处输入图像描述

4

3 回答 3

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 所确定的,为什么在第 1 节中,S13 的比赛 2 会在第 3 节之前到来。同样对于第 2 节,为什么 S14 会在第 2 场比赛的 S10 之前到来。

但是,我认为您现在可以修改上述查询以满足您的需要。

于 2013-08-05T12:43:32.787 回答
1

仅适用于第 1 节。您可以类似地为第 2 节和第 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小提琴

于 2013-08-05T11:52:58.797 回答
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
于 2013-08-05T11:27:42.307 回答