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 |
|
opensourcederry
Starting Member
12 Posts |
Posted - 2010-05-11 : 13:04:04
|
| Hi all,I have a killer query which is cracking me up.Imagine I have the simplified data set below. I need to go through a set of customer transactions and bolt on a new field that classifies them according to the following logicThe first trans is alway type C1If there is a 2nd transaction and it is within 25 days of the first then this is classed as C2. If it occurs more than 25 days then class this as type C1 and start again.If there is a third transaction and it occurs within 25 days of the second then class this as C3. If it occurs more than 25 days then class it as C1 and start again.If there is a 4th transaction (and up) and it is within 25 days of the third then class this as 'OS' If less than 25 days then class as C1 and start againAny ideas on how I can achieve this?Thanks,dsWITH temptrans AS(SELECT 1 [RN], 1234 [Accno], '01/01/2010' [TranDate] UNION ALL -- c1SELECT 2 [RN], 1234 [Accno], '15/01/2010' [TranDate] UNION ALL -- c2SELECT 3 [RN], 1234 [Accno], '01/03/2010' [TranDate] UNION ALL -- c1SELECT 4 [RN], 1234 [Accno], '15/04/2010' [TranDate] UNION ALL -- c1SELECT 5 [RN], 1234 [Accno], '23/04/2010' [TranDate] UNION ALL -- c2SELECT 6 [RN], 1234 [Accno], '27/04/2010' [TranDate] UNION ALL -- c3SELECT 7 [RN], 1234 [Accno], '29/04/2010' [TranDate])select * from temptrans |
|
|
asgast
Posting Yak Master
149 Posts |
Posted - 2010-05-12 : 08:00:52
|
| I came up with the following scriptI think that I got our logic correctly, but there a lot of place for optimization.;WITH temptrans AS(SELECT 1 [RN], 1234 [Accno], '2010-01-01 00:00:00' [TranDate] UNION ALL -- c1SELECT 2 [RN], 1234 [Accno], '2010-01-15 00:00:00' [TranDate] UNION ALL -- c2SELECT 3 [RN], 1234 [Accno], '2010-03-01 00:00:00' [TranDate] UNION ALL -- c1SELECT 4 [RN], 1234 [Accno], '2010-04-15 00:00:00' [TranDate] UNION ALL -- c1SELECT 5 [RN], 1234 [Accno], '2010-04-23 00:00:00' [TranDate] UNION ALL -- c2SELECT 6 [RN], 1234 [Accno], '2010-04-27 00:00:00' [TranDate] UNION ALL -- c3SELECT 7 [RN], 1234 [Accno], '2010-04-29 00:00:00' [TranDate])select * INTO #tempfrom temptransALTER TABLE #temp add class char(2)/*SEARCH for C1 */UPDATE t set class ='C1'FROM #temp tLEFT JOIN #temp t2 on datediff(day, t2.trandate,t.trandate)<25 AND t.rn <> t2.rn AND datediff(day, t2.trandate,t.trandate) >0 AND AND t.accno = t2.accnoWHERE t2.rn is nullUPDATE t set class ='C2'FROM #temp tLEFT JOIN #temp t2 on datediff(day, t2.trandate,t.trandate)<25 AND t.rn <> t2.rn AND datediff(day, t2.trandate,t.trandate) >0 AND t.accno = t2.accnoWHERE t2.class = 'C1'UPDATE t set class ='C3'FROM #temp tLEFT JOIN #temp t2 on datediff(day, t2.trandate,t.trandate)<25 AND t.rn <> t2.rn AND datediff(day, t2.trandate,t.trandate) >0 AND t.accno = t2.accnoWHERE t2.class = 'C2'UPDATE t set class ='OS'FROM #temp tLEFT JOIN #temp t2 on datediff(day, t2.trandate,t.trandate)<25 AND t.rn <> t2.rn AND datediff(day, t2.trandate,t.trandate) >0 AND t.accno = t2.accnoWHERE t2.class = 'C3'SELECT * FROM #tempDROP TABLE #temp |
 |
|
|
|
|
|
|
|