0

我的表的一种方式profile_test如下所示(列名显示在值的前面):

  1: profile1_test_1 153.3
  2: profile1_test_2 152.7
  3: profile1_test_3 151.5
  4: profile1_test_4 151.4
  5: profile1_test_5 151.7
  6: profile1_test_6 151.8
  7: profile1_test_7 156.7
  8: profile1_test_8 157.0
  9: profile1_test_9 156.8
  10: profile1_test_10 156.7

我想知道如何创建一个查询,该SQL查询将返回每一行(而不是整列)的和?数据库是。我想做的是不同列的平均值(它们是不同测试运行的结果,所以了解它们的 avg 和 std 很有趣)。AVGSTDMySQL

4

2 回答 2

0

这应该有效:

select rowid, avg(profile_test), stddev(profile_test)
from (select rowid,
             (case when n = 1 then profile1_test_1
                   when n = 2 then profile1_test_2
                   . . .
                   when n = 10 then profile1_test_10 
              end) as profile_test
      from profile_test cross join
           (select 1 as n union all select 2 union all select 3 union all select 4 union all select 5 union all
            select 6 union all select 7 union all select 8 union all select 9 union all select 10
           ) nums
      ) t
group by rowid

通过在每一行上编写复杂的算术可能有一种更有效的方法,但这应该适用于中等数据大小。

于 2013-04-04T00:42:47.650 回答
0

使用 UNION 将所有列放入子查询中的单个列中:

select run, avg(val), stddev(val)
from (select run, profile_test_1 val
      from mytable
      union
      select run, profile_test_2 val
      from mytable
      union
      select run, profile_test_3 val
      from mytable
      union
      ...) x
 group by run
于 2013-04-04T00:48:29.253 回答