简体   繁体   English

在sql中排成一行

[英]sum across a row in sql

I want to sum the values of Punten1,Punten 2,Punten 3,Punten4 as a total. 我想总结Punten1,Punten 2,Punten 3,Punten4的值。 But I only know how to sum the values across the column. 但我只知道如何对列中的值求和。 But not across the row. 但不是排在一排。 Does anyone know how to do this. 有谁知道如何做到这一点。

SELECT Punten1, Punten2, Punten3, Punten4
Punten1 + Punten 2 + Punten3 + Punten4 as Puntentotaal
FROM puntentotaal

So i would like to sum the values of Punten1,Punten 2,Punten 3,Punten4 in a new column: puntentotaal thanks! 所以我想在一个新专栏中总结Punten1,Punten 2,Punten 3,Punten4的值:puntentotaal谢谢!

SELECT Punten1, Punten2, Punten3, Punten4, 
Punten1 + Punten2 + Punten3 + Punten4 as Puntentotaal
FROM puntentotaal;

just an extra comma. 只是一个额外的逗号。 I am assuming your table as unique ID as a column. 我假设您的表作为列的唯一ID。

UPDATE puntentotaal AS t1 INNER JOIN puntentotaal AS t2 SET t1.Puntentotaal = 
(t2.Punten1 + t2.Punten2 + t2.Punten3 + t2.Punten4) where t1.ID = t2.ID;

This will update the sum of Punten1,Punten 2,Punten 3,Punten4 in column Puntentotaal in all the rows. 这将更新所有行中Puntentotaal列中的Punten1,Punten 2,Punten 3,Punten4的总和。

Your question titles as "sum across row" but what you are trying to achieve is sum across column. 您的问题标题为“行间总和”,但您要实现的是跨列的总和。

If you are looking for "sum across row" there is function called SUM(field_name) which will give you sum of specific field for all the rows selected, you can use this function with GROUP BY 如果您正在寻找“行间总和”,则有一个名为SUM(field_name)的函数,它将为您选择的所有行的特定字段的总和,您可以将此函数与GROUP BY一起使用

If you are looking for "sum across column" you are doing it right with + sign, just need to add comma as per @paul suggested. 如果你正在寻找“与列相加”,你正在使用+符号,只需要根据@paul建议添加逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM