总和

2015-05-09 49 views
0

我的表是:总和

我通过查询得到这个纪录:

var result = (from r in context.tbl_PlantDemand 
       join d in context.tbl_NoOfPlantDemanded on r.Id equals d.PlantDemandId 
       join c in context.Clones on d.CloneId equals c.Id 
       where r.PifId == pifId 
       select new 
        { 
         FinancialYear = r.FinancialYearVal, 
         TypeId = r.PlantationType, 
         Amt = d.Demand 
        }).ToList(); 

输出:

FinancialYear TypeId  Amt 
    --------------------------------- 
    2015-16   1   100 
    2015-16   1   50 
    2015-16   1   20 
    2016-17   2   100 
    2016-17   2   10 

我需要的总和结果Amt视组别而定2列& typeId

 FY TId Amt 
    -------------------- 
    2015-16 1  170 
    2016-17 2  110 

我可以很容易地按单列按列进行求和,但不知道如何求和多列。请让你不要被year有一群我知道,如果有人知道

+0

确定为第二,你需要130量不是110? –

+0

@学习请检查我已更新。你是对的 –

回答

1

找我:

select max([Year]), [Type Id], sum([Amt]) 
from Table1 
group by [Type Id] 

sql fiddle demo

+0

请我在c#aspx.cs页面添加了一个查询。所以我如何在C#中进行查询。 –