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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Number Events Occurring within a period

Author  Topic 

ucal
Yak Posting Veteran

72 Posts

Posted - 2008-03-17 : 16:11:15
I have TableA with the following structure and Data.


CaseID ActionID StartDate
------ ------------ --------------
A 23 2/21/2007
A 21 2/8/2007
B 22 12/4/2007
B 13 10/12/2007
B 17 10/7/2007
C 61 7/6/2007
D 33 11/2/2007
D 56 10/22/2007
D 26 7/29/2007
E 22 6/21/2007
E 58 5/25/2007
E 87 4/8/2007
E 29 3/23/2007

Expected Results:
I want to create TableB with the following structure and data .

CaseID X Y Z
----- - - -
A 0 0 1
B 0 1 1
C 0 0 0
D 0 0 1
E 1 1 1


X = 1 when a case has 4 or more actions within 180 days , 0 otherwise
Y= 1 when a case has 3 or more actions within 90 days , 0 otherwise
Z= 1 when a case has 2 or more actions within 30 days , 0 otherwise

Any help will be welcomed

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-03-17 : 17:35:05
Didn't we already do this?

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

ucal
Yak Posting Veteran

72 Posts

Posted - 2008-03-18 : 10:55:54
This is different
Go to Top of Page

ucal
Yak Posting Veteran

72 Posts

Posted - 2008-03-18 : 13:52:11
Can somebody please point me in the right direction
Go to Top of Page

ucal
Yak Posting Veteran

72 Posts

Posted - 2008-03-18 : 14:22:12
Jeff gave me this solution to solve similar problem.
How do I amend this to solve the current problem?

select t1.ID,
t1.ActionID,
t1.StartDate AS Date1,
t2.StartDate AS Date2,
abs(datediff(day, t1.StartDate, t2.StartDate)) as DaysDiff
from TT t1
inner join TT t2 on
t1.ID = t2.ID
and t1.StartDate <> t2.StartDate
ORDER BY
t1.ID,
t1.ActionID,
t1.StartDate
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-03-18 : 14:34:35
pretty similar
EDIT- to this: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=98559


select [caseID]
,X = case when X >= 4 then 1 else 0 end
,Y = case when Y >= 3 then 1 else 0 end
,Z = case when Z >= 2 then 1 else 0 end
from (
select [caseID]
,[X] = sum(case when datediff(day, startDate, getdate()) <= 180 then 1 else 0 end)
,[Y] = sum(case when datediff(day, startDate, getdate()) <= 90 then 1 else 0 end)
,[Z] = sum(case when datediff(day, startDate, getdate()) <= 30 then 1 else 0 end)
from TableA
group by [CaseID]
) d


Be One with the Optimizer
TG
Go to Top of Page

ucal
Yak Posting Veteran

72 Posts

Posted - 2008-03-18 : 14:43:23
Thanks TG,

The results from your T-SQL gives me the following results:

CaseID X Y Z
A 0 0 0
B 0 0 0
C 0 0 0
D 0 0 0
E 0 0 0

But the expected results should be :
CaseID X Y Z
----- - - -
A 0 0 1
B 0 1 1
C 0 0 0
D 0 0 1
E 1 1 1

Note : CaseID A has 2 actions (23,21) that occured within 30 days
hence Z =1 for CaseID A
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-03-18 : 15:10:43
CaseID A has only 1 action within 30 days (A 21 2/8/2007 - is more than 30 days ago)

similar problems with your other data. ie you show BY=1 (3 or more within 90) but you have only 3 Bs and 2 of them are more than 90 days ago...

Be One with the Optimizer
TG
Go to Top of Page

ucal
Yak Posting Veteran

72 Posts

Posted - 2008-03-18 : 15:17:13
CaseID A has action 21 on 2/8/2007 and action 23 on 2/21/2007.
Hence CaseID A has 2 actions within 30 days .
Thanks
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-03-18 : 15:28:46
Oh - When you said "2 or more actions within 30 days" I assumed you meant 2 or more actions within the past 30 days.


Be One with the Optimizer
TG
Go to Top of Page

ucal
Yak Posting Veteran

72 Posts

Posted - 2008-03-18 : 15:40:06
TG -I guess this should be some loop or cursor kind of thin to do
a multi comparison between the StartDate within each CaseID then Identify the number of actions that fall within each of the day groupings for each CaseID .
Now what is the way forward ..............................
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-03-18 : 16:09:55
cursor? nah. How about this:

declare @tableA table (caseid char(1), actionid int, startDate datetime)
insert @tableA
select 'A', 23, '2/21/2007' union all
select 'A', 21, '2/8/2007' union all
select 'B', 22, '12/4/2007' union all
select 'B', 13, '10/12/2007' union all
select 'B', 17, '10/7/2007' union all
select 'C', 61, '7/6/2007' union all
select 'D', 33, '11/2/2007' union all
select 'D', 56, '10/22/2007' union all
select 'D', 26, '7/29/2007' union all
select 'E', 22, '6/21/2007' union all
select 'E', 58, '5/25/2007' union all
select 'E', 87, '4/8/2007' union all
select 'E', 29, '3/23/2007'

;with yak (caseid, actionid, startdate, rn) as
(
select caseid, actionid, startdate, row_number() over (partition by caseid order by startdate)
from @tableA
)
select a.caseid
,max(case when datediff(day, a.startdate, x.startdate) <= 180 then 1 else 0 end) as Z
,max(case when datediff(day, a.startdate, y.startdate) <= 90 then 1 else 0 end) as Z
,max(case when datediff(day, a.startdate, z.startdate) <= 30 then 1 else 0 end) as Z
from yak a
left join yak z on a.caseid = z.caseid and z.rn = a.rn+1
left join yak y on a.caseid = y.caseid and y.rn = a.rn+2
left join yak x on a.caseid = x.caseid and x.rn = a.rn+3
group by a.caseid

output:
caseid Z Z Z
------ ----------- ----------- -----------
A 0 0 1
B 0 1 1
C 0 0 0
D 0 0 1
E 1 1 1


Be One with the Optimizer
TG
Go to Top of Page

ucal
Yak Posting Veteran

72 Posts

Posted - 2008-03-18 : 16:15:23
TG you are the best , this is exactly what I want.

Long live TG, Jeff and SQLTeam.
Go to Top of Page
   

- Advertisement -