用Sum选择Column

时间:2015-05-07 17:40:57

标签: sql

我有下表:

enter image description here

我有这个问题:

Select StartDate, SubscriptionGUID,
(Select sum(TotalExistingDBHoursInPeriod) 
from dbo.SQLDBBusinessKPIDB_Snap  
where [Platform] = 'SAWA') as "SAWA Contrib"
FROM dbo.SQLDBBusinessKPIDB_Snap 

返回以下结果: enter image description here

我想要的是" SAWA Contrib"要仅为SubscriptionGUID行的总和,它当前返回[Platform] = "SAWA"

所有行的总和

任何指针都会很棒!

1 个答案:

答案 0 :(得分:3)

向内部查询添加约束,如:

Select so.StartDate, so.SubscriptionGUID,
(Select sum(TotalExistingDBHoursInPeriod) 
from dbo.SQLDBBusinessKPIDB_Snap si
where si.[Platform] = 'SAWA' and si.SubscriptionGUID = so.SubscriptionGUID) as "SAWA Contrib"
FROM dbo.SQLDBBusinessKPIDB_Snap so