Author |
Topic |
srujanavinnakota
Starting Member
34 Posts |
Posted - 2010-09-09 : 14:48:06
|
Hi, Can anyone help with queryI have an audit table in the following format. Id customerId oldval newval Statusdate1 1 1 12 1/10/20102 1 12 53 1/12/20103 1 53 55 2/14/20104 1 55 57 3/15/20105 2 1 51 1/10/20106 2 51 53 3/15/20107 2 53 57 4/1/20108 3 1 51 6/1/20109 3 51 53 6/15/201010 3 53 56 7/14/201011 3 56 57 7/21/2010From the above table I need to pull the customer's most recent record that has oldval=53 and newval=57 and the immediate prior record to that record should be oldval=51 and newval=53.In other words records order should be like below oldval new val51 5353 57 |
|
MSquared
Yak Posting Veteran
52 Posts |
Posted - 2010-09-09 : 15:29:31
|
Try thisdeclare @t TABLE ( Id smallint, customerId smallint, oldval smallint, newval smallint, Statusdate smalldatetime)insert into @tselect 1, 1 ,1 ,12 ,'1/10/2010' union allselect 2, 1 ,12 ,53 ,'1/12/2010' union allselect 3, 1 ,53 ,55 ,'2/14/2010' union allselect 4, 1 ,55 ,57 ,'3/15/2010' union allselect 5, 2 ,1 ,51 ,'1/10/2010' union allselect 6, 2 ,51 ,53 ,'3/15/2010' union allselect 7 ,2 ,53 ,57 ,'4/1/2010' union allselect 8 ,3 ,1 ,51 ,'6/1/2010' union allselect 9 ,3 ,51 ,53 ,'6/15/2010' union allselect 10 ,3, 53 ,56 ,'7/14/2010' union allselect 11 ,3, 56, 57 ,'7/21/2010';with cte as(select CustomerID, OldVal, NewVal, ID from @t where oldval = 53 and newval = 57)select t.CustomerID, t.OldVal, t.NewVal, t.ID from @t t inner join cte c on t.customerId = c.customerId and t.Id = c.Id - 1Union all select CustomerID, OldVal, NewVal, ID from cte For Faster results please follow the posting guidelines herehttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2010-09-09 : 16:25:17
|
and the immediate prior record to that record should be oldval=51 and newval=53.Assume that it is based on statusdate column.Try this ..declare @t TABLE ( Id smallint, customerId smallint, oldval smallint, newval smallint, Statusdate smalldatetime)insert into @tselect 1, 1 ,1 ,12 ,'1/10/2010' union allselect 2, 1 ,12 ,53 ,'1/12/2010' union allselect 3, 1 ,53 ,55 ,'2/14/2010' union allselect 4, 1 ,55 ,57 ,'3/15/2010' union allselect 5, 2 ,1 ,51 ,'1/10/2010' union allselect 6, 2 ,51 ,53 ,'3/15/2010' union allselect 7 ,2 ,53 ,57 ,'4/1/2010' union allselect 8 ,3 ,1 ,51 ,'6/1/2010' union allselect 9 ,3 ,51 ,53 ,'6/15/2010' union allselect 10 ,3, 53 ,56 ,'7/14/2010' union allselect 11 ,3, 56, 57 ,'7/21/2010'-- borrow from MSquared;with temp as (select *, rn = row_number() over(order by statusdate) from @t)select t1.* from temp t1 join temp t2 on (t1.rn=t2.rn-1 and (t1.oldval=51 and t1.newval=53 and t2.oldval=53 and t2.newval=57)) or (t1.rn=t2.rn+1 and (t1.oldval=53 and t1.newval=57 and t2.oldval=51 and t2.newval=53))order by id |
 |
|
srujanavinnakota
Starting Member
34 Posts |
Posted - 2010-09-09 : 16:42:51
|
From the sample data I need get only customerID 2 records.Since the on 4/1/2010 values are oldval=53 and newval=57and immediate prior to that was on 3/15/2010 and values are oldval=51 and newval=53. it is based on statusdate column per customer. |
 |
|
namman
Constraint Violating Yak Guru
285 Posts |
Posted - 2010-09-09 : 22:52:00
|
declare @t TABLE (Id smallint, customerId smallint, oldval smallint, newval smallint, Statusdate smalldatetime)insert into @tselect 1, 1 ,1 ,12 ,'1/10/2010' union allselect 2, 1 ,12 ,53 ,'1/12/2010' union allselect 3, 1 ,53 ,55 ,'2/14/2010' union allselect 4, 1 ,55 ,57 ,'3/15/2010' union allselect 5, 2 ,1 ,51 ,'1/10/2010' union allselect 6, 2 ,51 ,53 ,'3/15/2010' union allselect 7 ,2 ,53 ,57 ,'4/1/2010' union allselect 8 ,3 ,1 ,51 ,'6/1/2010' union allselect 9 ,3 ,51 ,53 ,'6/15/2010' union allselect 10 ,3, 53 ,56 ,'7/14/2010' union allselect 11 ,3, 56, 57 ,'7/21/2010'-- borrow from MSquared;with temp as (select *, rn = row_number() over(partition by customerId order by statusdate) from @t)select t1.* from temp t1 join temp t2 on t1.customerId=t2.customerId and ((t1.rn=t2.rn-1 and (t1.oldval=51 and t1.newval=53 and t2.oldval=53 and t2.newval=57)) or (t1.rn=t2.rn+1 and (t1.oldval=53 and t1.newval=57 and t2.oldval=51 and t2.newval=53)))order by id--resultId customerId oldval newval Statusdate rn------ ---------- ------ ------ ----------------------- --------------------6 2 51 53 2010-03-15 00:00:00 27 2 53 57 2010-04-01 00:00:00 3 |
 |
|
|
|
|