[英]SQL: wrong number of arguments to function Sum()
I have a dataset with lots of columns. 我有一个包含很多列的数据集。 I need to do a
Group By ID
and then SUM
over all the other columns (So, ID
must NOT be aggregated). 我需要做一
Group By ID
,然后SUM
在所有其他列(因此, ID
必须不能聚合)。
Here is a small sample of my code: 这是我的代码的一小部分示例:
import pandas as pd
import pandasql as ps
dt= {
"ID" : [1,2,1,4,2],
"A" : [2,3,4,5,6],
"B":[10,20,30,40,50],
"C": [100, 200, 300, 400, 500]
}
dt= pd.DataFrame(dt)
dt= pd.DataFrame(dt)
query = """
select ID,Sum(*)
from dt
group by ID
"""
polyps = ps.sqldf(query)
But, it complains with: 但是,它抱怨:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) wrong number of arguments to function Sum()
[SQL:
select ID,Sum(*)
from dt
group by ID
]
So, a proper output is: 因此,正确的输出是:
ID A B C
0 1 6 40 400
1 2 9 70 700
2 4 5 700 400
If you want each column summed separately, you need to list them: 如果要分别汇总各列,则需要列出它们:
select ID, sum(a), sum(b), sum(c)
from dt
group by ID;
If you want the sum of all of them in one column: 如果要在一列中将它们的总和:
select ID, sum(a) + sum(b) + sum(c)
from dt
group by ID;
*
is only allowed for count(*)
where the purpose is to count rows. *
仅允许count(*)
用于计数行。
SELECT Id,(ISNULL(dt.val1,0) + ISNULL(dt.val2,0) + ISNULL(dt.val3,0)) as 'Total' from dt 从dt中将SELECT Id,(ISNULL(dt.val1,0)+ ISNULL(dt.val2,0)+ ISNULL(dt.val3,0))作为'Total'
The error "wrong number of arguments to function Sum()" says, instead of "*" you need to mention the fields inside SUM. 错误“函数Sum()的参数数量错误”表示,而不是“ *”,您需要提及SUM中的字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.