So I have this table (dml for sample data at end of post):CREATE TABLE #RxHistory ( rx_dosage_id int, member_id int, Drug_Name varchar(50), orderhistory_id int, ordercontent_id int, event varchar(10), NDC_Code varchar(11), NumDosages int)
If I order the data by member_id, Drug_Name, orderhistory_id, ordercontent_idSELECT * FROM #RxHistory order by member_id,Drug_Name,orderhistory_id,ordercontent_id
I want to find all rows that meet the following conditions:(I will write the condition in pseudo sql)where (event = 'Discontinu' and NumDosages = 1 -- here's the tricky part: and the event on the following row (based on the sort above) <> 'Active') or (event = 'Discontinu' and NumDosages > 1 -- more tricky stuff: and the event on the following row (based on the sort above) not in ('Active','Discontinu')
So if you look at the sample data sorted accordingly you will see that only the rows in red meet the criteria:rx_dosage_id member_id Drug_Name orderhistory_id ordercontent_id event NDC_Code NumDosages------------ ----------- ----------- --------------- --------------- ---------- ----------- -----------14044408 1059524 Haldol 7142769 13911534 Active 00045024560 114458708 1059524 Haldol 7366029 14330770 Change 00045024560 115374230 1059524 Haldol 7859455 15258370 Active 00045024560 114458708 1059524 Haldol 7859455 15258371 Discontinu 00045024560 115781433 1059524 Haldol 8077280 15670943 Reorder 00045024560 116121328 1059524 Haldol 8256696 16014500 Reorder 00045024560 13660192 1059528 Prozac 1690074 3507662 Active 00093719856 13849370 1059528 Prozac 1782997 3692445 Reorder 00093719856 14005000 1059528 Prozac 1858979 3844187 Change 00093719856 14290361 1059528 Prozac 1997468 4122340 Reorder 00093719856 14290361 1059528 Prozac 2053424 4232982 Discontinu 00093719856 13625208 1059530 Abilify 1672886 3473363 Active 12280001415 13758813 1059530 Abilify 1738706 3603958 Active 59148001013 13625208 1059530 Abilify 1783700 3693778 Discontinu 12280001415 13850748 1059530 Abilify 1783700 3693784 Reorder 59148001013 13964912 1059530 Abilify 1839665 3805178 Reorder 59148001013 14115746 1059530 Abilify 2009596 4146569 Discontinu 59148001013 19716528 1059534 Chantix 4834297 9546212 Active 00069046856 19716529 1059534 Chantix 4834297 9546213 Active 00069046856 19716530 1059534 Chantix 4834297 9546214 Active 00069046956 19716528 1059534 Chantix 5580243 10960689 Discontinu 00069046856 18085314 1059540 Geodon 3968063 7905515 Active 00049397041 28085315 1059540 Geodon 3968063 7905515 Active 00049397041 28455544 1059540 Geodon 4164735 8278463 Active 00049397041 28455545 1059540 Geodon 4164735 8278463 Active 00049397041 28085314 1059540 Geodon 4323743 8578948 Discontinu 00049397041 28085315 1059540 Geodon 4323743 8578948 Discontinu 00049397041 28754187 1059540 Geodon 4323747 8578955 Change 00049397041 28754188 1059540 Geodon 4323747 8578955 Change 00049397041 28754187 1059540 Geodon 4419493 8760003 Discontinu 00049397041 28754188 1059540 Geodon 4419493 8760003 Discontinu 00049397041 27144661 1059557 Doxepin 3468462 6956705 Active 00049535066 17144661 1059557 Doxepin 4153504 8257601 Discontinu 00049535066 18434879 1059557 Doxepin 4153504 8257604 Active 00049536066 18434879 1059557 Doxepin 4462011 8840779 Discontinu 00049536066 1
So I need to solve the "tricky stuff" from my pseudo sql above.Thanks,LaurieSample data:INSERT INTO #RxHistorySELECT 14044408,1059524,'Haldol',7142769,13911534,'Active','00045024560',1 UNION ALLSELECT 14458708,1059524,'Haldol',7366029,14330770,'Change','00045024560',1 UNION ALLSELECT 15374230,1059524,'Haldol',7859455,15258370,'Active','00045024560',1 UNION ALLSELECT 14458708,1059524,'Haldol',7859455,15258371,'Discontinu','00045024560',1 UNION ALLSELECT 15781433,1059524,'Haldol',8077280,15670943,'Reorder','00045024560',1 UNION ALLSELECT 16121328,1059524,'Haldol',8256696,16014500,'Reorder','00045024560',1 UNION ALLSELECT 3660192,1059528,'Prozac',1690074,3507662,'Active','00093719856',1 UNION ALLSELECT 3849370,1059528,'Prozac',1782997,3692445,'Reorder','00093719856',1 UNION ALLSELECT 4005000,1059528,'Prozac',1858979,3844187,'Change','00093719856',1 UNION ALLSELECT 4290361,1059528,'Prozac',1997468,4122340,'Reorder','00093719856',1 UNION ALLSELECT 4290361,1059528,'Prozac',2053424,4232982,'Discontinu','00093719856',1 UNION ALLSELECT 3625208,1059530,'Abilify',1672886,3473363,'Active','12280001415',1 UNION ALLSELECT 3758813,1059530,'Abilify',1738706,3603958,'Active','59148001013',1 UNION ALLSELECT 3625208,1059530,'Abilify',1783700,3693778,'Discontinu','12280001415',1 UNION ALLSELECT 3850748,1059530,'Abilify',1783700,3693784,'Reorder','59148001013',1 UNION ALLSELECT 3964912,1059530,'Abilify',1839665,3805178,'Reorder','59148001013',1 UNION ALLSELECT 4115746,1059530,'Abilify',2009596,4146569,'Discontinu','59148001013',1 UNION ALLSELECT 9716528,1059534,'Chantix',4834297,9546212,'Active','00069046856',1 UNION ALLSELECT 9716529,1059534,'Chantix',4834297,9546213,'Active','00069046856',1 UNION ALLSELECT 9716530,1059534,'Chantix',4834297,9546214,'Active','00069046956',1 UNION ALLSELECT 9716528,1059534,'Chantix',5580243,10960689,'Discontinu','00069046856',1 UNION ALLSELECT 8085314,1059540,'Geodon',3968063,7905515,'Active','00049397041',2 UNION ALLSELECT 8085315,1059540,'Geodon',3968063,7905515,'Active','00049397041',2 UNION ALLSELECT 8455544,1059540,'Geodon',4164735,8278463,'Active','00049397041',2 UNION ALLSELECT 8455545,1059540,'Geodon',4164735,8278463,'Active','00049397041',2 UNION ALLSELECT 8085314,1059540,'Geodon',4323743,8578948,'Discontinu','00049397041',2 UNION ALLSELECT 8085315,1059540,'Geodon',4323743,8578948,'Discontinu','00049397041',2 UNION ALLSELECT 8754187,1059540,'Geodon',4323747,8578955,'Change','00049397041',2 UNION ALLSELECT 8754188,1059540,'Geodon',4323747,8578955,'Change','00049397041',2 UNION ALLSELECT 8754187,1059540,'Geodon',4419493,8760003,'Discontinu','00049397041',2 UNION ALLSELECT 8754188,1059540,'Geodon',4419493,8760003,'Discontinu','00049397041',2 UNION ALLSELECT 7144661,1059557,'Doxepin' ,3468462,6956705,'Active','00049535066',1 UNION ALLSELECT 7144661,1059557,'Doxepin',4153504,8257601,'Discontinu','00049535066',1 UNION ALLSELECT 8434879,1059557,'Doxepin' ,4153504,8257604,'Active','00049536066',1 UNION ALLSELECT 8434879,1059557,'Doxepin' ,4462011,8840779,'Discontinu','00049536066',1