SQL查询 - 同一个表的更高级别的总和

时间:2016-02-16 20:41:26

标签: sql sql-server sum

我在SQL Server数据库中有数据,这个查询为我提供了顶级表

SELECT date, resource_name, transfer_resource_name, SUM(calls) as calls 
FROM abc

我无法弄清楚如何填充total_calls列,如下图所示。基本上,我需要将选择日期,resource_name和total_calls列的调用和组相加。

任何输入都将受到赞赏!

Here is a picture of the tables

2 个答案:

答案 0 :(得分:4)

您的查询在SQL Server中不起作用,因此我假设实际查询具有group by

然后你可以使用窗口函数做你想做的事情:

SELECT date, resource_name, transfer_resource_name, SUM(calls) as calls,
       SUM(SUM(calls)) OVER (PARTITION BY resource_name) as total_calls 
FROM abc
GROUP BY date, resource_name, transfer_resource_name;

答案 1 :(得分:0)

使用子查询。

private void Validate<C>() where C : class
{
    Validate(typeof(C));
} 

private void Validate(Type type)
{
    MemberInfo[] x = type.GetMembers();
    for (int i = 0; i < x.Length; i++)
    {
        if (x[i].MemberType != MemberTypes.Field && x[i].MemberType != MemberTypes.NestedType)
        {
            throw new Exception(string.Format("Class members must be of type Field or NestedType"));
        }

        foreach (var n in x.GetNestedTypes())
            Validate(n);
    }
}