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 |
stamford
Starting Member
47 Posts |
Posted - 2014-05-08 : 06:46:13
|
I need a script that will return CARE_ID, EVENT_DATE, EVENT_ID, EVENT_TYPE from this table.For each CARE_ID I need the last CP before the first appearance of 'BR', 'CH', 'SU' or 'TE' The tie breaker for 'CP' is MAX(EVENT_ID) and for 'BR', 'CH', 'SU' or 'TE') it is MIN(EVENT_ID) For each CARE_ID that isn't included above return the most recent 'CP' The tie breaker for 'CP' is MAX(EVENT_ID).So I'm thinking two scripts with a UNION?Two examples are as below. * The returned row for CARE_ID 3 has a 'CP' before the first 'BR', 'CH', 'SU' or 'TE'* The returned row for CARE_ID 78 does not have a 'CP' before the first 'BR', 'CH', 'SU' or 'TE' so the most recent CP has been returned.CARE_ID EVENT_DATE EVENT_ID EVENT_TYPE3 09/04/2010 00:00 117 CP78 09/12/2010 00:00 707 CP The script has to be fairly old school CARE_ID EVENT_DATE EVENT_ID EVENT_TYPE3 09/04/2010 00:00 117 CP3 11/04/2010 00:00 104 CH3 16/04/2010 00:00 190 SU3 12/07/2010 00:00 16 BR3 13/07/2010 00:00 17 BR3 13/07/2010 00:00 18 BR78 27/07/2009 00:00 11 CH78 28/07/2009 00:00 9 TE78 08/12/2010 00:00 706 CP78 09/12/2010 00:00 707 CP107 23/02/2010 00:00 93 CP107 21/09/2012 00:00 1474 SU364 26/01/2011 00:00 1122 CP364 18/02/2011 00:00 1136 CP364 19/02/2011 00:00 569 SU364 23/08/2012 00:00 774 CH367 21/06/2010 00:00 151 CP367 01/07/2010 00:00 247 SU653 30/09/2010 00:00 360 SU653 15/10/2010 00:00 295 CP653 15/10/2010 00:00 298 CP655 06/06/2011 00:00 1570 CP689 06/10/2010 00:00 372 SU689 26/10/2010 00:00 307 CP689 02/11/2010 00:00 306 CP689 02/11/2010 00:00 370 CP689 26/08/2011 00:00 1944 CP689 27/08/2011 00:00 1947 CP689 22/09/2011 00:00 2125 CP689 24/09/2012 00:00 2654 CP689 24/09/2012 00:00 2677 CP689 06/11/2012 00:00 2720 CP689 11/11/2012 00:00 2736 CP689 13/11/2012 00:00 2752 CP689 15/11/2012 00:00 2765 CP1066 11/11/2010 00:00 472 SU1066 15/11/2010 00:00 91 TE1066 23/11/2010 00:00 408 CP3907 05/01/2014 00:00 3591 CP3907 09/01/2014 00:00 945 CH3907 13/01/2014 00:00 1821 SU3907 14/01/2014 00:00 3592 CP3907 14/01/2014 00:00 3593 CP |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-05-08 : 07:52:40
|
[code];with aCTEAS ( select 3 CARE_ID,'09/04/2010 00:00' EVENT_DATE ,117 EVENT_ID,'CP' EVENT_TYPE UNION ALL select 3,'11/04/2010 00:00',104,'CH' union all select 3,'16/04/2010 00:00',190,'SU' union all select 3,'12/07/2010 00:00',16,'BR' union all select 3,'13/07/2010 00:00',17,'BR' union all select 3,'13/07/2010 00:00',18,'BR' union all select 78,'27/07/2009 00:00',11,'CH' union all select 78,'28/07/2009 00:00',9,'TE' union all select 78,'08/12/2010 00:00',706,'CP' union all select 78,'09/12/2010 00:00',707,'CP')select CARE_ID ,EVENT_DATE ,EVENT_ID ,EVENT_TYPE from( select A.* ,Row_Number() OVER(Partition By A.CARE_ID Order by A.EVENT_DATE desc) as rn from aCTE A outer apply ( select Top 1 EVENT_DATE from aCTE B where EVENT_TYPE in ('BR', 'CH', 'SU' , 'TE' ) AND A.CARE_ID=B.CARE_ID Order by EVENT_DATE ASC ) C Where A.EVENT_DATE<C.EVENT_DATE AND A.EVENT_TYPE='CP')XWhere X.rn=1[/code]output[code]CARE_ID EVENT_DATE EVENT_ID EVENT_TYPE3 09/04/2010 00:00 117 CP78 09/12/2010 00:00 707 CP[/code]sabinWeb MCP |
|
|
|
|
|
|
|