sql表中跨行求和

sql表中跨行求和,sql,Sql,我对sql比较陌生,所以这可能是一个简单的问题。我有一张表格,每天记录每个人的个人数字。它看起来像这样 person1 | person2 | person3 | Date __________________________________ 2 | 4 | 5 | 12/2/2015 __________________________________ 3 | 5 | 6 | 12/1/2015 我想做的是添加另一列,列

我对sql比较陌生,所以这可能是一个简单的问题。我有一张表格,每天记录每个人的个人数字。它看起来像这样

person1 | person2 | person3 | Date
__________________________________
2       | 4       | 5       | 12/2/2015
__________________________________
3       |  5      |  6      | 12/1/2015
我想做的是添加另一列,列出每天完成的总数。大概是这样的:

person1 | person2 | person3 | Date       | Total completed
___________________________________________________________
2       | 4       | 5       | 12/2/2015  | 11
___________________________________________________________
3       |  5      |  6      | 12/1/2015  | 14

我知道如何在列上使用
SUM()
,但在行上似乎不起作用。非常感谢您的帮助。

在表中添加另一列作为
Total\u completed
,并执行如下更新声明

update your_table
set total_completed = person1+person2+person3
select person1,person2,person3,[date],person1+person2+person3 as total from 
your_table
但是,如果希望选择相同的列而不向表中添加新列,则只需执行如下select语句即可

update your_table
set total_completed = person1+person2+person3
select person1,person2,person3,[date],person1+person2+person3 as total from 
your_table

在表中添加另一列作为
Total_completed
,并执行如下更新语句

update your_table
set total_completed = person1+person2+person3
select person1,person2,person3,[date],person1+person2+person3 as total from 
your_table
但是,如果希望选择相同的列而不向表中添加新列,则只需执行如下select语句即可

update your_table
set total_completed = person1+person2+person3
select person1,person2,person3,[date],person1+person2+person3 as total from 
your_table

假设person1、person2、person3列是相同的数据类型(INT?),下面的查询应该可以做到这一点

SELECT person1, person2, person3, Date, person1+person2+person3 as TotalCompleted
FROM yourtable

假设person1、person2、person3列是相同的数据类型(INT?),下面的查询应该可以做到这一点

SELECT person1, person2, person3, Date, person1+person2+person3 as TotalCompleted
FROM yourtable

我认为您提出的问题突出了一个关于表和数据库设计的更深层次的问题,特别是与规范化概念相关的问题

如果每天完成任务的人数发生变化,会发生什么情况?如果只有一到两个人在完成任务,你只需填写一些列就可以了,但是如果你有三个人以上,会发生什么呢?添加其他列意味着访问该表的任何查询都需要重写以利用新列,并且需要更新具有三个或更少条目的表条目,以使用值填充新列(或适当地处理空值)

我建议使用一种更像以下内容的表结构:

table PEOPLE:
personID integer, 
(...other information about the person)

table PEOPLE_TASKS_COMPLETED:
personID integer, (this will point at a row in the PEOPLE table)
tasksCompleted integer,
dateCompleted date
select dateCompleted, SUM(tasksCompleted)
from PEOPLE_TASKS_COMPLETED
GROUP BY dateCompleted
通过这种方式分解数据,您可以使用如下查询查找任意数量的人在特定日期完成的任务总数:

table PEOPLE:
personID integer, 
(...other information about the person)

table PEOPLE_TASKS_COMPLETED:
personID integer, (this will point at a row in the PEOPLE table)
tasksCompleted integer,
dateCompleted date
select dateCompleted, SUM(tasksCompleted)
from PEOPLE_TASKS_COMPLETED
GROUP BY dateCompleted

我认为您提出的问题突出了一个关于表和数据库设计的更深层次的问题,特别是与规范化概念相关的问题

如果每天完成任务的人数发生变化,会发生什么情况?如果只有一到两个人在完成任务,你只需填写一些列就可以了,但是如果你有三个人以上,会发生什么呢?添加其他列意味着访问该表的任何查询都需要重写以利用新列,并且需要更新具有三个或更少条目的表条目,以使用值填充新列(或适当地处理空值)

我建议使用一种更像以下内容的表结构:

table PEOPLE:
personID integer, 
(...other information about the person)

table PEOPLE_TASKS_COMPLETED:
personID integer, (this will point at a row in the PEOPLE table)
tasksCompleted integer,
dateCompleted date
select dateCompleted, SUM(tasksCompleted)
from PEOPLE_TASKS_COMPLETED
GROUP BY dateCompleted
通过这种方式分解数据,您可以使用如下查询查找任意数量的人在特定日期完成的任务总数:

table PEOPLE:
personID integer, 
(...other information about the person)

table PEOPLE_TASKS_COMPLETED:
personID integer, (this will point at a row in the PEOPLE table)
tasksCompleted integer,
dateCompleted date
select dateCompleted, SUM(tasksCompleted)
from PEOPLE_TASKS_COMPLETED
GROUP BY dateCompleted
复制品