Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
my table
SQL
vID     Amt

2	50
2	60
4	120
4	110
5	50
5	70



how to get result

XML
vID     Amt

2   10
4   10
5   20
Posted
Comments
pradiprenushe 25-Nov-14 5:26am    
any Logic? why 2 & 4 having 10
and why 5 is having 20
rajveer344 25-Nov-14 5:28am    
No, but there is always two value for each vid's. & i want to get their differnce.

try this..
SQL
select vID,max(isnull(amt,0))-min(isnull(amt,0)) as SubValue from [dbo].[My_Table] group by vID
 
Share this answer
 
v3
Comments
King Fisher 25-Nov-14 5:54am    
Well :)
aarif moh shaikh 25-Nov-14 5:58am    
Good one...
check this, its kind of running sum requirement you have.

Use-Of-Self-Joins-Instead-Of-Cursor-Or-While-Loop[^]

Instead of adding , write the logic of subtraction.

Also you can do this

SQL
select vID  ,   Amt, Row_Number() Over (Partition by Vid Order by Vid) As ROWID into #temp
from table

select b.Vid,B.Amt-A.amt
from
(select * from #temp Where ROWID=2 ) b
Join 
(select * from #temp Where ROWID=1 ) a
On b.Vid=a.Vid
 
Share this answer
 
v2
Try this
SQL
select vid, max(Amt) - min(Amt) from table group by vid;
 
Share this answer
 
Try this:

SQL
select vID ,MAX(Amt)-MIN(Amt) from #temp group by vID     
 
Share this answer
 

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