Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2004-06-23 : 10:39:49
|
Oz writes "Hey guys,I have a question for youGiven below is my table in sql server... if I want to calculate the difference between one row and the previous row based on the transaction date how could I do that. Num. catcode dsc trans_date trans_mth clm_amt1 A01070 clm1 2000-01-01 00:00:00 01 20002 A01070 clm1 2000-01-03 00:00:00 01 30003 A01070 clm1 2000-01-15 00:00:00 01 40004 A01070 clm1 2000-02-05 00:00:00 02 10005 A01070 clm2 2000-03-05 00:00:00 03 1000 Hint: I have done this based on the number and the sql is as below... but, I can't get my head around getting this working based on the transaction dateselect t1.num, t1.catcode, t1.dsc, t1.trans_date, t1.trans_mth, sum(t1.clm_amt) clm_amt, sum(t1.clm_amt-isnull(t2.clm_amt,0)) new_clm_amtfrom clm_test t1 left outer join clm_test t2 ont1.num = t2.num+1 and t1.trans_mth = t2.trans_mthgroup by t1.num, t1.catcode, t1.dsc, t1.trans_date, t1.trans_mthorder by t1.num, t1.catcode, t1.dsc, t1.trans_date, t1.trans_mthcheers for ur help guys" |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-06-23 : 11:15:57
|
| maybe this:select t1.num, t1.catcode, t1.dsc, t1.trans_date, t1.trans_mth, sum(t1.clm_amt) clm_amt, sum(t1.clm_amt-isnull(t2.clm_amt,0)) new_clm_amtfrom clm_test t1 left outer join clm_test t2 ont1.trans_date = DATEADD(day, 1, t2.trans_date)and t1.trans_mth = t2.trans_mthgroup by t1.num, t1.catcode, t1.dsc, t1.trans_date, t1.trans_mthorder by t1.num, t1.catcode, t1.dsc, t1.trans_date, t1.trans_mthand why do you need trans_mth when you have it in the trans_date column??Go with the flow & have fun! Else fight the flow :) |
 |
|
|
Pat Phelan
Posting Yak Master
187 Posts |
Posted - 2004-06-23 : 17:53:54
|
I think that you could use:SELECT a.trans_date, a.clm_amount - b.clm_amount FROM clm_test AS a JOIN clm_test AS b ON (b.clm_date = (SELECT Max(c.clm_date) FROM clm_test AS c WHERE c.clm_date < a.clm_date)) -PatP |
 |
|
|
|
|
|
|
|