2

I want to get the sum of many columns after its group its like this here is my table

  Name     |  Department | basic salary | EPF   | ETF  |
 -------------------------------------------------------
 Prasad      Head Office     25000        1200    800
 sean        Head Office     25000        1200    800
------------------------------------------------------
Total                        50000        2400    1600  // I want To get This Row In between every                   
------------------------------------------------------     Department Chage.How to add this row
 John        X1              30000        1500    950
 karl        x1              20000        1000    700
 mena        x1              10000         500    250
-----------------------------------------------------
Total                        60000        3000   1900
-----------------------------------------------------
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 3
    How do you want to do that? It is SQL or C# code? And please show us what you tried – Johann Blais Sep 08 '14 at 09:39
  • Similar question, not sure if answer there is useful - http://stackoverflow.com/questions/17813580/add-a-new-row-to-datatable-for-sub-total – Bulat Sep 08 '14 at 10:11

2 Answers2

0

Try Below Query may help you

select isnull(e.EmpName,'Total'),D.Dname Department,
sum(E.BasicSalary),sum(E.EPF),sum(E.ETF)
from Employee E inner join Department D on D.DeptNo=E.DeptNo
group by  D.Dname,e.EmpName with rollup
0

Sounds like you need to this sort of thing:

foreach(var grp in employees.GroupBy(x => x.Department)) 
{
  foreach(var emp in grp)
  {
     Console.WriteLine(String.Join("\t", emp.Name, emp.Department, emp.BasicSalary, emp.EPF, emp.ETF));
  }
  Console.WriteLine(String.Join("\t", "Total", "", grp.Sum(x => x.BasicSalary), grp.Sum(x => x.EPF), grp.Sum(x => x.ETF)));
}

i.e. group by the department then for each group iterate the employees within the group, followed by the sums for the group.

stovroz
  • 6,835
  • 2
  • 48
  • 59