MySQL查询平均值

时间:2013-10-19 12:09:09

标签: mysql average

早上好。我有这张桌子:

mysql> select * from Data;
+---------------------------+--------+-------+
| affyId                    | exptId | level |
+---------------------------+--------+-------+
| 31315_at                  | 3      |   250 |
| 31324_at                  | 3      |    91 |
| 31325_at                  | 1      |   191 |
| 31325_at                  | 2      |   101 |
| 31325_at                  | 4      |    51 |
| 31325_at                  | 5      |    71 |
| 31325_at                  | 6      |    31 |
| 31356_at                  | 3      |    91 |
| 31362_at                  | 3      |   260 |
| 31510_s_at                | 3      |   257 |
| 5321_at                   | 4      |    90 |
| 5322_at                   | 4      |    90 |
| 5323_at                   | 4      |    90 |
| 5324_at                   | 3      |    57 |
| 5324_at                   | 4      |    90 |
| 5325_at                   | 4      |    90 |
| AFFX-BioB-3_at            | 3      |    97 |
| AFFX-BioB-5_at            | 3      |    20 |
| AFFX-BioB-M_at            | 3      |    20 |
| AFFX-BioB-M_at            | 5      |   214 |
| AFFX-BioB-M_at            | 7      |    20 |
| AFFX-BioB-M_at            | 8      |    40 |
| AFFX-BioB-M_at            | 9      |    20 |
| AFFX-HSAC07/X00351_M_at   | 3      |    86 |
| AFFX-HUMBAPDH/M33197_3_st | 3      |   277 |
| AFFX-HUMTFFR/M11507_at    | 3      |    90 |
| AFFX-M27830_3_at          | 3      |   271 |
| AFFX-MurIL10_at           | 3      |     8 |
| AFFX-MurIL10_at           | 5      |     8 |
| AFFX-MurIL10_at           | 6      |     4 |
| AFFX-MurIL2_at            | 3      |    20 |
| AFFX-MurIL4_at            | 5      |    78 |
| AFFX-MurIL4_at            | 6      |    20 |
| U95-32123_at              | 1      |   128 |
| U95-32123_at              | 2      |   128 |
| U98-40474_at              | 1      |    57 |
| U98-40474_at              | 2      |    57 |
+---------------------------+--------+-------+
37 rows in set (0.00 sec)

如果我想在所有实验中寻找每个阵列探针(affyId)的平均表达水平(水平),我会SELECT affyId, AVG(level) AS average FROM Data GROUP BY affyId;

但是,我无法弄清楚如何为每个实验寻找每个数组探针(affyId)的平均表达水平......它必须与上一个查询类似,但我没有获得好的结果......有什么帮助吗?

PD:有人告诉我,如果有人解决我的问题,我应该给一些声誉或点击一些绿色按钮......是不是?我该怎么做?我在这个网站上很新......

2 个答案:

答案 0 :(得分:0)

只需将其添加到group by子句

即可
SELECT affyId, exptId, AVG(level) AS average 
FROM Data
GROUP BY affyId, exptId;

答案 1 :(得分:0)

这显示了每个affyId的平均值:

SELECT affyId, AVG(level) AS average FROM Data GROUP BY affyId

这是每个exptId的平均值:

SELECT exptId, AVG(level) AS average FROM Data GROUP BY exptId

这是每个affyId中每个exptId的平均值:

SELECT affyId, exptId, AVG(level) AS average FROM Data GROUP BY exptId, affyId