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 - 2005-12-09 : 06:49:26
|
| a2l2en writes "This may stump you. ;-)I need to write a query that pulls transactions that have happened for any userid, every day for a consecutive number of days.I need the userid, countofconsecutivedays, transaction_type.The format of the transactions are as follows:userid date transaction223344 Jan 6, 2005 jkl;75499 Jan 1, 2001 jkl;223344 Jan 7, 2005 asdfAny ideas?" |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-09 : 09:04:52
|
| The sample data are not enough to give the answer. Post some sample data and the result you wantMadhivananFailing to plan is Planning to fail |
 |
|
|
druer
Constraint Violating Yak Guru
314 Posts |
Posted - 2005-12-09 : 10:33:33
|
| The following may get you started while I continue working. At this point it will identify the transactions that have a match the following day, and will return them all. At this point it doesn't count the number of consecutive days for it though it just shows those that have the same thing on the next day.declare @mytable TABLE(userid int, actdate datetime, trantype char(1))insert into @mytable select 1, '1/1/05', 'A' UNION ALLselect 1, '1/3/05', 'B' UNION ALLselect 1, '1/4/05', 'B' UNION ALLselect 1, '2/1/05', 'A' UNION ALLselect 2, '2/1/05', 'C' UNION ALLselect 2, '2/2/05', 'C' UNION ALLselect 2, '2/3/05', 'C' select t1.userid, t1.actdate, t1.trantypefrom @mytable t1 inner join @mytable t2 ont2.userid = t1.useridand t2.trantype = t1.trantypeand datediff(day, t1.actdate, t2.actdate) = 1 |
 |
|
|
druer
Constraint Violating Yak Guru
314 Posts |
Posted - 2005-12-09 : 11:48:13
|
| This works. Probably could be improved but it does the trick.set nocount ondeclare @mytable TABLE(userid int, actdate datetime, trantype char(1))insert into @mytable select 1, '1/1/05', 'A' UNION ALLselect 1, '1/3/05', 'B' UNION ALLselect 1, '1/4/05', 'B' UNION ALLselect 1, '2/1/05', 'A' UNION ALLselect 2, '2/1/05', 'C' UNION ALLselect 2, '2/2/05', 'C' UNION ALLselect 2, '2/3/05', 'C' UNION ALLselect 2, '2/4/05', 'C' UNION ALLselect 2, '2/6/05', 'C' UNION ALLselect 2, '2/7/05', 'C' declare @myResults table (id int identity(1,1), userid int, actdate datetime, trantype char(1), numtimes int)-- pulls all of the ones that have a matching transaction after it into table variableinsert into @myResultsselect t1.userid, t1.actdate, t1.trantype, 0from @mytable t1 inner join @mytable t2 ont2.userid = t1.useridand t2.trantype = t1.trantypeand datediff(day, t1.actdate, t2.actdate) = 1-- update the numtimes field to 2 if there is no transaction after it in the sequenceupdate @myResultsset numtimes = 2from @myResults t1 where not exists(select id from @myresults t2 where t2.id = t1.id + 1 and t2.trantype = t1.trantype and datediff(day, t1.actdate, t2.actdate) = 1)-- loop that finds all cases where numtimes is 0 but next item matches and moves the counter down to -- the previous one and increments the number by 1 each timedeclare @id intselect @id = t1.id from @myresults t1 inner join @myresults t2 on t2.id = t1.id + 1 and t2.userid = t1.userid and datediff(day, t1.actdate, t2.actdate) = 1 where t2.numtimes > 0 and t1.numtimes = 0while (NOT @id IS NULL )begin -- update the numtimes to the next id's value + 1 update t1 set numtimes = t2.numtimes + 1 from @myresults t1 inner join @myresults t2 on t2.id = @id + 1 and t2.userid = t1.userid where t1.id = @id -- set the id that had a numtimes to 0 so it won't be counted anymore update @myresults set numtimes = 0 where id = @id + 1 -- clear the @id variable and see if it can find anymore with later sequences select @id = NULL select @id = t1.id from @myresults t1 inner join @myresults t2 on t2.id = t1.id + 1 and t2.userid = t1.userid and datediff(day, t1.actdate, t2.actdate) = 1 where t2.numtimes > 0 and t1.numtimes = 0end-- Return the resultsselect userid, actdate, trantype, numtimesfrom @myresults where numtimes > 0 |
 |
|
|
|
|
|
|
|