一个Group By的聚合和求和

时间:2014-11-07 15:40:24

标签: sql sql-server view

我有一个视图,我从我的数据库中获取一些值和差异。我使用这段代码:

Select DATEDIFF(minute, 0, DATEADD(day, 0, t1.HorasEfe)) as Soma, t1.IDDiligencia
from DiligenciaSub t1
group by t1.IDDiligencia,t1.HorasEfe
order by t1.HorasEfe

我得到这个作为输出:

enter image description here

我需要什么:

总结IDDiligencia等于的Soma的值!

是否可以调整我的实际查询来执行此操作?

4 个答案:

答案 0 :(得分:2)

只需将HorasEfe从群组中删除,然后添加SUM

Select SUM(DATEDIFF(minute, 0, DATEADD(day, 0, t1.HorasEfe))) as Soma, t1.IDDiligencia
from DiligenciaSub t1
group by t1.IDDiligencia

答案 1 :(得分:0)

Select DATEDIFF(minute, 0, DATEADD(day, 0, t1.HorasEfe)) as Soma, t1.IDDiligencia
from DiligenciaSub t1
group by t1.IDDiligencia
order by DATEDIFF(minute, 0, DATEADD(day, 0, t1.HorasEfe))                                 

答案 2 :(得分:0)

Select 
SUM(Select (DATEDIFF(minute, 0, DATEADD(day, 0, t1.HorasEfe))) from DiligenciaSub t1) as Soma,
t1.IDDiligencia
from DiligenciaSub t1
group by t1.IDDiligencia,t1.HorasEfe
order by t1.HorasEfe

答案 3 :(得分:0)

从原始查询中删除sum和group by

将结果插入临时表#t

然后使用此查询

select sum(soma) as sum_soma,IDDiligencia as idd
from #t
group by IDDiligencia

您将获得以下结果

enter image description here