Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何汇总多个平均列?_Python_Sqlite - Fatal编程技术网

Python 如何汇总多个平均列?

Python 如何汇总多个平均列?,python,sqlite,Python,Sqlite,现在我正在做以下工作: c.execute("SELECT AVG(col1) from table") avg_c1 = c.fetchall()[0] c.execute("SELECT AVG(col2) from table") avg_c2 = c.fetchall()[0] c.execute("SELECT AVG(col3) from table") avg_c3 = c.fetchall()[0] c.execute("SELECT AVG(col4) from table")

现在我正在做以下工作:

c.execute("SELECT AVG(col1) from table")
avg_c1 = c.fetchall()[0]
c.execute("SELECT AVG(col2) from table")
avg_c2 = c.fetchall()[0]
c.execute("SELECT AVG(col3) from table")
avg_c3 = c.fetchall()[0]
c.execute("SELECT AVG(col4) from table")
avg_c4 = c.fetchall()[0]
c.execute("SELECT AVG(col5) from table")
avg_c5 = c.fetchall()[0]
c.execute("SELECT AVG(col6) from table")
avg_c6 = c.fetchall()[0]
c.execute("SELECT AVG(col1), AVG(col2), AVG(col3), AVG(col4) from table")
avg_c = c.fetchall();
我想通过总结这些重复的行来缩短代码。但是我试过了

c.execute("SELECT AVG(col1,col2,col3,col4,col5,col6) from table")
avg = c.fetchall()[0]
并且它表示
AVG()
的参数数目错误


格式化代码的正确方法是什么?谢谢

您可以使用
UNION ALL

SELECT AVG(col)
FROM (
  SELECT col1 AS col FROM tab UNION ALL
  SELECT col2        FROM tab UNION ALL
  ...
  SELECT col6        FROM tab
) s

假设它是同一个表,则可以执行以下操作:

c.execute("SELECT AVG(col1) from table")
avg_c1 = c.fetchall()[0]
c.execute("SELECT AVG(col2) from table")
avg_c2 = c.fetchall()[0]
c.execute("SELECT AVG(col3) from table")
avg_c3 = c.fetchall()[0]
c.execute("SELECT AVG(col4) from table")
avg_c4 = c.fetchall()[0]
c.execute("SELECT AVG(col5) from table")
avg_c5 = c.fetchall()[0]
c.execute("SELECT AVG(col6) from table")
avg_c6 = c.fetchall()[0]
c.execute("SELECT AVG(col1), AVG(col2), AVG(col3), AVG(col4) from table")
avg_c = c.fetchall();

您可以对每个查询进行多个聚合,如:

c.execute("SELECT AVG(col1), AVG(col2), AVG(col3), AVG(col4), AVG(col5), AVG(col6) FROM table")
avg = c.fetchall()[0]
c.execute(“从表中选择平均值(col1)、平均值(col2)、平均值(col3)、平均值(col4)、平均值(col5)、平均值(col6))
avg=c.fetchall()[0]
聚合函数本身并不一定是相同的。例如,您可以选择
col1
的平均值,以及
col2
的最小值,等等


在同一个查询中使用多个聚合通常比分别使用一个特定聚合进行查询要好,因为到数据库的“往返”次数减少了。

您可以计算一列平均值(col)并且不能提供所有逗号分隔的列名。嗯,谢谢,但我不明白代码的意思或正在做什么。我在其他问题上看到了这个答案,并认为这是为了其他目的。有什么简单的解释吗?或者你能给我指一些参考资料来研究吗?谢谢@LeongJunHao它基本上将6列数据转换为单个列,然后计算单个平均值