Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a Table like this
Id	name	salary
1	A	100
2	B	200
3	A	100
4	C	700
5	D	600
6	C	700


I require this result using SQL query like this
name	salary	sumofsalary
A	100	100
B	200	300
A	100	400
C	700	1100
D	600	1700
C	700	2400


Please see every next row sum of salary is done with the all previous row salaries

I got the solution to get the required result with inner join but i am not able to understand the behavior of query. I mean how it is being done with inner join.

The query is:

SQL
select e.name,e.salary,sum(e2.Salary) sal
from Emp e inner join Emp e2
on e.Id>=e2.Id
group by e.Id,e.name,e.salary



Please help
Posted
Updated 27-Sep-13 23:49pm
v3

Think of joining two tables on employeed id - one which is used to compute the sum and then joined to the other which contains all details.
 
Share this answer
 
u should have tried Google
Check this links in cp: Visual Representation of SQL Joins
Search join in cp
 
Share this answer
 
Hi...

Check in Code project site it self....
In the below link.. you will get the required Answer(In my Post)
Accounts calculation for small office accounts management[^]
IN SQL Server, First row Last Column is How to add with second row Columns?[^]
(Note: Not able to Solve this in JOIN Concept)
Regards,
GVPrabu
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900