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 |
pras2007
Posting Yak Master
216 Posts |
Posted - 2009-10-04 : 15:29:50
|
Hello All,I have been thinking of the best way to solve my problem and I need help. The sample table below illustrates the final output. Based on certain criteria I will update the table accordingly…First I will look at the processdate field, doing so I can determine the previous record populate into that table. The problem is that the processdate field is not in sequence meaning that I can have anywhere 1 – 6 months difference between pervious process dates. I need a way to figure the previous processdate programmatically.Second, once I determine the previous process date I will compare the record, account_num, and INC fields to determine if it is a new entry to the table if so, I will update the following:col_1_rev = col_1col_2_rev = col_2col_3_rev = col_3If record, account_num, and INC fields is not a new entry I will do the following:col_1_rev = 0col_2_rev = 0col_3_rev = 0Does anyone know how to accomplish this task? Please advice.Thanks. |
|
pras2007
Posting Yak Master
216 Posts |
Posted - 2009-10-04 : 15:31:39
|
The result table is below:ProcessDate Record account_Num INC Col_1 Col_2 Col_3 Col_1_Rev Col_2_Rev Col_3_Rev7/31/2007 273 03001S 0 950 50 1000 950 50 10008/31/2007 273 03001S 0 950 50 1000 0 0 09/30/2007 273 03001S 0.5 8000 0 8000 8000 0 800011/30/2007 273 03001S 1 200 100 300 200 100 3002/28/2008 273 03001S 1 200 100 300 0 0 0 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-10-05 : 14:19:21
|
[code]UPDATE tSET t.Col_1_Rev=CASE WHEN t1.Col1 IS NULL THEN t.Col_1 ELSE 0 END,t.Col_2_Rev=CASE WHEN t1.Col2 IS NULL THEN t.Col_2 ELSE 0 END,t.Col_3_Rev=CASE WHEN t1.Col3 IS NULL THEN t.Col_3 ELSE 0 ENDFROM table tOUTER APPLY (SELECT TOP 1 Col_1,Col_2,Col_3 FROM Table WHERE record =t.record AND account_num=t.account_num AND INC=t.INC AND ProcessDate < t.ProcessDate ORDER BY ProcessDate DESC)t1[/code] |
|
|
|
|
|
|
|