TSQL对一列2的每个值对一列1的所有值求和,并且仅显示一列2的值与列1的总和

时间:2018-08-09 18:13:02

标签: sql-server tsql

我想对所有数字求和,而不管它来自哪个服务器,并显示每个CreateDate的总和。

[Message] [char](30) NOT NULL,
[Value] [bigint] NOT NULL,
[ServerName] [char](20) NOT NULL,
[CreateDate] [smalldatetime] NOT NULL

Message     Value   ServerName  CreateDate
ttl        1        server1     2018-08-09 17:58:00
ttl        1        server2     2018-08-09 17:58:00
ttl        2        server3     2018-08-09 17:58:00
ttl        3        server1     2018-08-09 17:59:00
ttl        3        server1     2018-08-09 17:59:00

所以结果看起来像这样。

Value     CreateDate
4       2018-08-09 17:58:00
6       2018-08-09 17:59:00

如果我愿意

Select sum(value), CreateDate as time
from table
group by Value, CreateDate
order by CreateDate

我仍然看到CreateDate的重复条目。我想要一个CreateDate值,以及该CreateDate值的所有求和值。

2 个答案:

答案 0 :(得分:1)

关闭,从“分组依据”中删除“值”字段。

 Select sum(value), CreateDate as time
    from table
    group by CreateDate
    order by CreateDate

答案 1 :(得分:1)

只需从“分组依据”中删除“值”:

Select sum(value), CreateDate as time
from table
group by CreateDate
order by CreateDate