For the following table I need two scripts to extract two subsequent tables.The resulting scripts have to be fairly old school and be backwards compatible with SQL2000.Any advice is much appreciated.Firstly for each value of CARE_ID I need to return all rows where the EVENT_TYPE is in (CH, TE, BR) and the EVENT_DATE is the last date before the EVENT_DATE of the row where the EVENT_TYPE is SU. There will only ever be one instance of SU for each CARE_ID.For example - for CARE_ID 6460 this is the row that will be returnedCARE_ID EVENT_ID EVENT_DATE EVENT_TYPE6460 1699 05/01/2014 00:00 CH
Secondly what I need is very similar to the above but I need the first instance where the EVENT_TYPE is in (CH, TE, BR) and the EVENT_DATE is the first date after the EVENT_DATE for EVENT_TYPE = SU.For example - for CARE_ID 6460 this is the row that will be returnedCARE_ID EVENT_ID EVENT_DATE EVENT_TYPE6460 1698 12/03/2014 00:00 CH
In both cases if there are multiple rows which cannot be separated by EVENT_DATE because they have the EVENT_DATE on the same date, then the tie breaker is the EVENT_ID. For instances before SU the EVENT_ID value should be the highest, and for instances after SU the EVENT_ID value should be the lowest.CARE_ID EVENT_ID EVENT_DATE EVENT_TYPE3 189 15/04/2010 00:00 SU3 104 12/05/2010 00:00 CH3 16 12/07/2010 00:00 BR3 17 13/07/2010 00:00 BR3 18 13/07/2010 00:00 BR39 6 21/07/2009 00:00 CH39 45 09/10/2009 00:00 TE78 9 28/07/2009 00:00 TE78 11 28/07/2009 00:00 CH95 21 31/07/2009 00:00 CH95 21 31/07/2009 00:00 TE151 49 21/03/2010 00:00 TE217 158 18/02/2010 00:00 SU217 102 30/03/2010 00:00 CH355 19 13/07/2010 00:00 BR355 20 13/07/2010 00:00 BR355 21 13/07/2010 00:00 BR355 22 13/07/2010 00:00 BR355 23 13/07/2010 00:00 BR355 56 16/07/2010 00:00 TE355 111 16/07/2010 00:00 CH364 569 29/01/2011 00:00 SU364 673 24/08/2012 00:00 TE367 247 01/07/2010 00:00 SU369 117 28/07/2010 00:00 CH369 248 28/07/2010 00:00 SU380 277 08/07/2011 00:00 TE481 63 07/09/2010 00:00 TE481 289 07/09/2010 00:00 SU536 135 10/09/2010 00:00 CH568 314 14/09/2010 00:00 SU631 147 25/12/2009 00:00 CH631 74 15/01/2010 00:00 TE631 148 02/02/2010 00:00 CH653 360 30/09/2010 00:00 SU680 366 30/09/2010 00:00 SU689 372 06/10/2010 00:00 SU811 413 27/10/2010 00:00 SU1026 1637 12/04/2012 00:00 SU1066 472 11/11/2010 00:00 SU1066 91 15/11/2010 00:00 TE1109 958 19/07/2011 00:00 SU1193 2368 27/03/2013 00:00 SU1199 501 19/11/2010 00:00 SU1506 178 01/02/2011 00:00 TE1506 329 01/02/2011 00:00 CH1506 330 02/02/2011 00:00 CH1506 166 03/02/2011 00:00 TE1506 167 04/02/2011 00:00 TE1506 333 05/02/2011 00:00 CH1506 170 07/02/2011 00:00 TE1506 173 05/03/2011 00:00 TE1506 174 05/03/2011 00:00 TE1506 175 07/03/2011 00:00 TE1506 176 07/03/2011 00:00 TE1506 336 07/03/2011 00:00 CH1506 337 07/03/2011 00:00 CH1506 339 07/03/2011 00:00 CH1506 341 07/03/2011 00:00 CH1623 531 27/01/2011 00:00 SU1661 216 25/01/2011 00:00 CH1661 217 25/01/2011 00:00 CH1661 95 01/02/2011 00:00 TE1661 218 01/02/2011 00:00 CH1662 220 25/01/2011 00:00 CH1662 221 01/02/2011 00:00 CH1662 743 13/01/2012 00:00 CH1662 744 23/01/2012 00:00 CH1662 672 28/02/2012 00:00 CH1662 751 27/03/2012 00:00 CH1662 752 27/03/2012 00:00 CH1662 753 27/03/2012 00:00 CH1662 754 27/03/2012 00:00 CH1662 755 27/03/2012 00:00 CH1662 756 27/03/2012 00:00 CH1662 757 27/03/2012 00:00 CH1662 758 27/03/2012 00:00 CH1662 759 27/03/2012 00:00 CH1662 760 27/03/2012 00:00 CH1662 761 27/03/2012 00:00 CH1663 97 25/01/2011 00:00 TE1663 98 01/02/2011 00:00 TE1663 223 01/02/2011 00:00 CH1666 100 23/01/2011 00:00 TE1666 225 01/02/2011 00:00 CH1667 227 01/02/2011 00:00 CH1753 326 16/02/2011 00:00 CH1753 327 17/02/2011 00:00 CH1753 165 19/02/2011 00:00 TE1753 168 20/02/2011 00:00 TE1753 331 20/02/2011 00:00 CH1753 334 21/02/2011 00:00 CH1753 171 22/02/2011 00:00 TE1792 188 04/03/2011 00:00 TE1792 189 05/03/2011 00:00 TE1792 349 05/03/2011 00:00 CH1792 191 06/03/2011 00:00 TE1792 351 06/03/2011 00:00 CH1805 378 05/03/2011 00:00 CH1805 383 05/03/2011 00:00 CH1805 379 06/03/2011 00:00 CH1805 381 07/03/2011 00:00 CH1841 597 28/02/2011 00:00 SU1887 607 18/03/2011 00:00 SU1903 657 26/01/2012 00:00 CH1938 251 01/05/2011 00:00 TE2054 780 12/05/2011 00:00 SU2123 769 05/05/2011 00:00 SU2123 280 10/07/2011 00:00 TE2186 802 16/05/2011 00:00 SU2186 1113 23/08/2012 00:00 CH2186 1114 23/08/2012 00:00 CH2186 1115 23/08/2012 00:00 CH2186 1116 23/08/2012 00:00 CH2187 803 29/01/2011 00:00 SU2189 227 16/05/2011 00:00 TE2189 228 16/05/2011 00:00 TE2222 434 11/05/2011 00:00 CH2222 513 15/06/2011 00:00 CH2265 448 31/05/2011 00:00 CH2294 66 12/06/2011 00:00 BR2306 918 06/05/2011 00:00 SU2306 558 11/09/2011 00:00 CH2306 559 13/09/2011 00:00 CH2306 560 15/09/2011 00:00 CH2306 561 17/09/2011 00:00 CH2364 411 24/04/2012 00:00 TE2437 518 01/07/2011 00:00 CH2457 105 01/12/2011 00:00 BR2457 649 01/12/2011 00:00 CH2476 1039 17/08/2011 00:00 SU2512 1393 08/08/2011 00:00 SU2516 1378 27/01/2012 00:00 SU2630 576 01/10/2011 00:00 CH2630 320 30/10/2011 00:00 TE2630 319 05/11/2011 00:00 TE2630 321 06/11/2011 00:00 TE2630 577 08/11/2011 00:00 CH2630 318 09/11/2011 00:00 TE2630 316 15/11/2011 00:00 TE2630 317 15/11/2011 00:00 TE2630 578 15/11/2011 00:00 CH2630 579 15/11/2011 00:00 CH2694 1112 30/04/2011 00:00 SU2694 307 15/06/2011 00:00 TE2694 544 02/07/2011 00:00 CH2694 82 14/10/2011 00:00 BR2761 1145 11/10/2011 00:00 SU2778 1147 11/10/2011 00:00 SU2784 1157 03/10/2011 00:00 SU2787 1158 12/09/2011 00:00 SU2804 1175 25/10/2011 00:00 SU2807 1394 01/02/2012 00:00 SU2869 581 15/09/2011 00:00 CH2869 322 18/09/2011 00:00 TE2869 323 21/09/2011 00:00 TE2869 84 25/09/2011 00:00 BR2869 85 29/09/2011 00:00 BR2875 583 14/11/2011 00:00 CH2920 1323 15/12/2011 00:00 SU2974 1250 25/11/2011 00:00 SU3052 340 05/11/2011 00:00 TE3052 624 18/12/2011 00:00 CH3063 629 01/12/2011 00:00 CH3081 1314 20/12/2011 00:00 SU3084 352 01/12/2011 00:00 TE3114 1358 05/01/2012 00:00 SU3133 644 03/01/2012 00:00 CH3152 1345 12/01/2012 00:00 SU3152 372 18/01/2012 00:00 TE3152 648 18/01/2012 00:00 CH3152 110 30/01/2012 00:00 BR3190 1360 05/01/2012 00:00 SU3213 377 20/01/2012 00:00 TE3213 1373 20/01/2012 00:00 SU3318 1420 16/02/2012 00:00 SU3337 676 01/01/2012 00:00 CH3337 678 29/02/2012 00:00 CH3344 2120 17/03/2012 00:00 SU3365 696 10/11/2011 00:00 CH3365 704 01/12/2011 00:00 CH3416 1508 17/03/2012 00:00 SU3454 766 17/04/2012 00:00 CH3454 408 13/05/2012 00:00 TE3496 1558 12/03/2012 00:00 SU3515 789 15/06/2012 00:00 TE3515 2700 16/09/2013 00:00 SU3621 802 02/06/2012 00:00 CH3621 809 07/06/2012 00:00 CH3637 811 12/06/2012 00:00 CH3664 431 02/07/2012 00:00 TE3694 828 17/05/2012 00:00 CH3773 1196 30/01/2013 00:00 CH3773 2701 10/02/2013 00:00 SU3784 902 01/07/2012 00:00 CH3797 544 09/07/2012 00:00 TE3803 1752 07/06/2011 00:00 SU3858 1810 22/04/2012 00:00 SU3858 590 01/05/2012 00:00 TE3920 1056 01/07/2012 00:00 CH3920 1058 02/07/2012 00:00 CH3920 1060 03/07/2012 00:00 CH3920 1062 04/07/2012 00:00 CH3920 1064 05/07/2012 00:00 CH3920 276 06/07/2012 00:00 BR3920 650 07/07/2012 00:00 TE3920 652 15/07/2012 00:00 TE3920 278 17/07/2012 00:00 BR3920 654 18/07/2012 00:00 TE3920 656 21/07/2012 00:00 TE3928 1078 01/07/2012 00:00 CH3928 1139 02/10/2012 00:00 CH3945 1088 21/06/2012 00:00 CH3975 1966 11/08/2012 00:00 SU3975 668 13/08/2012 00:00 TE3975 1096 13/08/2012 00:00 CH3976 1097 23/12/2009 00:00 CH3984 1972 16/08/2012 00:00 SU3984 681 25/08/2012 00:00 TE3984 1136 29/08/2012 00:00 CH4043 2015 18/03/2012 00:00 SU4043 1131 21/03/2012 00:00 CH4043 678 24/05/2012 00:00 TE4126 2082 22/10/2012 00:00 SU4220 349 30/04/2013 00:00 BR4307 2179 05/12/2012 00:00 SU4307 1256 06/02/2013 00:00 CH4314 2185 11/12/2012 00:00 SU4333 2216 31/12/2012 00:00 SU4431 2268 27/08/2012 00:00 SU4541 2285 01/02/2013 00:00 SU4541 1688 04/03/2014 00:00 CH4670 2276 06/01/2011 00:00 SU4711 2300 01/02/2012 00:00 SU4711 1257 07/02/2012 00:00 CH4730 1262 22/11/2012 00:00 CH4730 2303 30/11/2012 00:00 SU4740 760 18/02/2013 00:00 TE4740 1274 18/02/2013 00:00 CH4740 346 30/04/2013 00:00 BR4740 347 30/04/2013 00:00 BR4810 2652 27/01/2013 00:00 SU4863 1322 22/11/2012 00:00 CH4863 2412 11/12/2012 00:00 SU4866 1413 07/07/2013 00:00 CH4866 831 13/08/2013 00:00 TE4871 2401 15/03/2013 00:00 SU4872 2403 11/03/2013 00:00 SU4876 1324 13/04/2013 00:00 CH4894 1325 22/11/2012 00:00 CH4930 1330 21/11/2012 00:00 CH4930 2425 11/12/2012 00:00 SU4936 2428 20/04/2013 00:00 SU4937 2430 22/04/2013 00:00 SU4961 1335 22/11/2012 00:00 CH4961 2447 11/12/2012 00:00 SU4993 2463 15/01/2013 00:00 SU4993 1345 25/01/2013 00:00 CH4997 1354 22/11/2012 00:00 CH4997 2471 11/12/2012 00:00 SU5049 1371 28/05/2013 00:00 CH5200 1410 22/11/2012 00:00 CH5200 2604 11/12/2012 00:00 SU5225 2802 01/11/2013 00:00 SU5227 2607 03/07/2013 00:00 SU5298 2647 08/08/2013 00:00 SU5298 1454 11/09/2013 00:00 CH5298 1458 12/09/2013 00:00 CH5298 1496 30/09/2013 00:00 CH5333 2660 30/07/2013 00:00 SU5340 1589 27/12/2013 00:00 CH5380 1494 26/09/2013 00:00 CH5500 2735 26/09/2013 00:00 SU5500 1585 19/12/2013 00:00 CH5538 2755 28/08/2013 00:00 SU5553 2796 01/10/2013 00:00 SU5562 1517 11/10/2013 00:00 CH5586 2771 15/10/2013 00:00 SU5600 2776 03/02/2013 00:00 SU5600 1521 21/03/2013 00:00 CH5796 855 24/07/2013 00:00 TE5798 1549 02/11/2013 00:00 CH5798 2851 04/11/2013 00:00 SU5816 856 24/07/2013 00:00 TE5818 2832 03/11/2013 00:00 SU5843 1546 02/11/2013 00:00 CH5870 1561 11/12/2013 00:00 CH5870 2877 11/12/2013 00:00 SU5890 1578 18/12/2013 00:00 CH5924 2946 23/04/2013 00:00 SU5925 2949 23/12/2013 00:00 SU6050 2995 16/01/2014 00:00 SU6054 3039 15/01/2014 00:00 SU6106 3018 16/01/2014 00:00 SU6217 3077 28/01/2014 00:00 SU6217 873 04/02/2014 00:00 TE6234 878 13/02/2014 00:00 TE6264 882 20/02/2014 00:00 TE6264 1653 20/02/2014 00:00 CH6273 3105 15/02/2014 00:00 SU6281 3111 29/03/2012 00:00 SU6281 886 01/05/2012 00:00 TE6281 1660 01/05/2012 00:00 CH6288 399 24/02/2014 00:00 BR6288 400 24/02/2014 00:00 BR6288 401 24/02/2014 00:00 BR6300 3122 06/02/2009 00:00 SU6300 1673 01/03/2009 00:00 CH6327 3130 10/01/2013 00:00 SU6407 1696 14/03/2014 00:00 CH6452 3181 17/03/2014 00:00 SU6460 1699 05/01/2014 00:00 CH6460 3201 10/01/2014 00:00 SU6460 1698 12/03/2014 00:00 CH6464 1701 18/12/2013 00:00 CH