| 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/2007A 21 2/8/2007B 22 12/4/2007B 13 10/12/2007B 17 10/7/2007C 61 7/6/2007D 33 11/2/2007D 56 10/22/2007D 26 7/29/2007E 22 6/21/2007E 58 5/25/2007E 87 4/8/2007E 29 3/23/2007Expected Results:I want to create TableB with the following structure and data .CaseID X Y Z----- - - -A 0 0 1B 0 1 1C 0 0 0D 0 0 1E 1 1 1X = 1 when a case has 4 or more actions within 180 days , 0 otherwiseY= 1 when a case has 3 or more actions within 90 days , 0 otherwiseZ= 1 when a case has 2 or more actions within 30 days , 0 otherwiseAny help will be welcomed |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-03-18 : 10:55:54
|
| This is different |
 |
|
|
ucal
Yak Posting Veteran
72 Posts |
Posted - 2008-03-18 : 13:52:11
|
| Can somebody please point me in the right direction |
 |
|
|
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 DaysDifffrom TT t1 inner join TT t2 on t1.ID = t2.ID and t1.StartDate <> t2.StartDateORDER BY t1.ID, t1.ActionID, t1.StartDate |
 |
|
|
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=98559select [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 endfrom ( 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 OptimizerTG |
 |
|
|
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 ZA 0 0 0B 0 0 0C 0 0 0D 0 0 0E 0 0 0But the expected results should be :CaseID X Y Z----- - - -A 0 0 1B 0 1 1C 0 0 0D 0 0 1E 1 1 1Note : CaseID A has 2 actions (23,21) that occured within 30 days hence Z =1 for CaseID A |
 |
|
|
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 OptimizerTG |
 |
|
|
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 |
 |
|
|
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 OptimizerTG |
 |
|
|
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 doa 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 .............................. |
 |
|
|
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 @tableAselect 'A', 23, '2/21/2007' union allselect 'A', 21, '2/8/2007' union allselect 'B', 22, '12/4/2007' union allselect 'B', 13, '10/12/2007' union allselect 'B', 17, '10/7/2007' union allselect 'C', 61, '7/6/2007' union allselect 'D', 33, '11/2/2007' union allselect 'D', 56, '10/22/2007' union allselect 'D', 26, '7/29/2007' union allselect 'E', 22, '6/21/2007' union allselect 'E', 58, '5/25/2007' union allselect 'E', 87, '4/8/2007' union allselect '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 Zfrom yak aleft join yak z on a.caseid = z.caseid and z.rn = a.rn+1left join yak y on a.caseid = y.caseid and y.rn = a.rn+2left join yak x on a.caseid = x.caseid and x.rn = a.rn+3group by a.caseidoutput:caseid Z Z Z------ ----------- ----------- -----------A 0 0 1B 0 1 1C 0 0 0D 0 0 1E 1 1 1 Be One with the OptimizerTG |
 |
|
|
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. |
 |
|
|
|