特定行值组

时间:2017-05-13 12:10:29

标签: sql

我的表格与下面的格式一样,我想为test1和test2的显示列平均值编写sql语句,为main和列的位置编写列。enter image description here

我试过这个sql:

select name, average as final,AVG(case when exam in('test1','test2') ) as testa 
from table 
where exam='main' 
order by main final 
group by name; 

但它不起作用....请帮忙 我希望结果看起来像这样: enter image description here

4 个答案:

答案 0 :(得分:0)

您可以执行以下操作。

Select t1.name, average12, main, rownum from (
    select t1.name as name, t1.avg(average) as average12, t2.mainexam as main 
    from <table> t1 left join <table> t2 
    on t1.id = t2.id 
    where t2.exam = 'Main' 
    group by t1.name, t2.mainexam
) order by main desc;

答案 1 :(得分:0)

尝试SQL Server的此查询:

SELECT 
      e.name AS 'Student Name'
    , CAST( AVG( CASE WHEN e.Exam IN ( 'Test1', 'Test2' ) THEN e.average END ) AS FLOAT ) AS 'Test Average'
    , e2.average AS 'Main Exam'
    , ROW_NUMBER() OVER ( ORDER BY e2.average DESC ) AS 'Position'

FROM 
    exams AS e

    INNER JOIN exams AS e2
        ON e2.name = e.name
        AND e2.exam = 'Main'

GROUP BY 
      e.name
    , e2.average

ORDER BY 
    e2.average DESC

输出是:

+--------------+--------------+-----------+----------+
| Student Name | Test Average | Main Exam | Position |
+--------------+--------------+-----------+----------+
| Name1        |         60.5 |        89 |        1 |
| Name2        |         49.5 |        63 |        2 |
| Name3        |           73 |        38 |        3 |
+--------------+--------------+-----------+----------+

答案 2 :(得分:0)

这个简单的查询平均了Test1和Test2。根据您的dbms,您可能会遇到 name 等保留字的问题。

select name, cast(avg(average) as integer)
from test
where exam = 'Test1'
   or exam = 'Test2'
group by name;

这个简单的查询为您提供“main”的值。

select name, average as main
from test
where exam = 'Main'

将它们放在一个公用表表达式中,然后加入“name”。

with test_avg as (
  select name, cast(avg(average) as integer)
  from test
  where exam = 'Test1'
     or exam = 'Test2'
group by name
), main_avg as (
  select name, average as main
  from test
  where exam = 'Main'
)
select t1.name, t1.avg, t2.main
from test_avg t1
inner join main_avg t2
on t1.name = t2.name;
name      avg  main
--
Name1     61   89
Name2     50   63
Name3     73   38

我不知道位置的含义。如果您尝试将行从最高平均排名到最低平均值,您可能会这样做。

with test_avg as (
  select name, cast(avg(average) as integer)
  from test
  where exam = 'Test1'
     or exam = 'Test2'
group by name
), main_avg as (
  select name, average as main
  from test
  where exam = 'Main'
)
select t1.name, t1.avg, t2.main, row_number() over (order by avg desc) as position
from test_avg t1
inner join main_avg t2
on t1.name = t2.name
order by position;
name      avg  main  position
--
Name3     73   38    1
Name1     61   89    2
Name2     50   63    3

答案 3 :(得分:0)

你可以在这里玩最大值....非常简单

select a.*,row_number() over (order by main desc) as position from
 (
    select name,
    AVG(case when exam in ('Test1','Test2') then Average else null end ) as testa,
    max(case when exam in ('Main') then Average else null end) as main
    from abc 
    group by name
) a
    order by name;

输出:

name    testa   main    position1
Name1   60.50   89        1
Name2   49.50   63        2
Name3   73.00   38        3