|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Query Help: Looking for Nextdate based on many prev date fieldsCurrently I run a query that returns these columns and I manually scan the data to look for issues. I would like to be able to have a NextAction date column, perhaps a case, but what would the construct look like? -- ddl create table #sam (id uniqueidentiifier ,recrecd datetime ,AppRecd datetime ,examDt datetime ,letter varchar(2) ,conciergedt datetime ,initltr datetime ,firstf datetime ,secondf datetime ,noapp datetime ,finalf datetime ,apprecdt datetime ,paidt datetime) /* Workflow recrecd = data entry date AppRecd = application received date examDt = exam received date letter = code as number or characters, which determins content of the client letters conciergedt = welcome letter sent to all new clients initltr = date first formal letter is sent 2 days after the concierge letter firstf = first Followup 14 days after initLtr secondf = second Followup 14 days after the firstF noapp = we received the exam but not the application occurs when this is true finalf = final followup 14 days after the second and sets initLtr = TO (timed out) apprecdt = application received date paidt = date payment was received An automated process fills in most of the dates when it prints the letters. */ drop #sam -- end snipit TIA JeffP... Hi
It is not clear what you are trying to achieve, but maybe thos could help! If you had a table such as CREATE TABLE Actions ( actiontype int, [description] varchar(20), daysdifference int ) CREATE TABLE LetterActions ( id uniqueidentifier, actiontype int, actiondate datetime ) INSERT INTO Actions ( actiontype , [description] , daysdifference ) SELECT 1, 'DateCreated', 0 UNION ALL SELECT 2, 'Stage1Date', 14 UNION ALL SELECT 3, 'Stage2Date', 7 UNION ALL SELECT 4, 'Stage3Date', 21 DELETE FROM LetterActions INSERT INTO LetterActions ( id, actiontype, actiondate ) SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 1, '20050101' UNION ALL SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 2, '20050106' UNION ALL SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 3, '20050120' UNION ALL SELECT '32224215-DCDD-5A27-93CA-DB80A52F1234', 1, '20050103' UNION ALL SELECT '32224215-DCDD-5A27-93CA-DB80A52F1234', 2, '20050110' UNION ALL SELECT '54444244-AADD-4F98-7899-DD23252F1234', 1, '20050113' UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 1, '20050102' UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 2, '20050112' UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 3, '20050118' UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 4, '20050123' SELECT l.id, l.ActionType AS lastAction, l.actiondate AS lastactiondate, n.actionType AS nextaction, DATEADD(d,N.daysdifference,l.actiondate) as NextActionDate FROM LetterActions l JOIN ( SELECT id, Max(Actiontype) as ActionType FROM LetterActions GROUP BY id ) M ON L.id = M.id and L.actiontype = M.actiontype LEFT JOIN Actions N ON N.ActionType = ( SELECT Min(ActionType) FROM Actions A WHERE A.Actiontype > L.ActionType ) John Show quote "JDP@Work" wrote: > How would I make a query to know what the next step is? > > Currently I run a query that returns these columns and I manually scan the data > to look for issues. > > I would like to be able to have a NextAction date column, perhaps a case, but > what would the construct look like? > > -- ddl > create table #sam (id uniqueidentiifier ,recrecd datetime > ,AppRecd datetime ,examDt datetime > ,letter varchar(2) ,conciergedt datetime ,initltr datetime > ,firstf datetime ,secondf datetime ,noapp datetime > ,finalf datetime ,apprecdt datetime ,paidt datetime) > > /* Workflow > > recrecd = data entry date > AppRecd = application received date > examDt = exam received date > letter = code as number or characters, which determins content of the client > letters > conciergedt = welcome letter sent to all new clients > initltr = date first formal letter is sent 2 days after the concierge letter > firstf = first Followup 14 days after initLtr > secondf = second Followup 14 days after the firstF > noapp = we received the exam but not the application occurs when this is true > finalf = final followup 14 days after the second and sets initLtr = TO (timed > out) > apprecdt = application received date > paidt = date payment was received > > An automated process fills in most of the dates when it prints the letters. */ > > drop #sam > -- end snipit > > TIA > > JeffP... > > > John, thanks for your attempt, but I'm not sure I made myself clear.
I don't have one lastdate/action date field, I have many. The more I think about it, I may write a case stmt to handle all the IIF's I was thinking that there was a way to eval a group of columns at once similar to what coalesce does to return the first none null value. I may also implement a new field that is updated each time a workflow process is executed This will be my last date and action as one varchar field. I can then cast the date portion back to a date and the action to a datediff value to test against. This will form the basis for an exception query to find records that are not processed in the "normal" order and to find records that have fallen off the workflow track. Here's the rub, I have many more fields and events that I didn't include, so the complexity goes & grows. TIA JeffP.... Show quote "John Bell" <jbellnewspo***@hotmail.com> wrote in message news:87345044-088F-4BD3-B33C-41CCF5B06A1C@microsoft.com... > Hi > > It is not clear what you are trying to achieve, but maybe thos could help! > > If you had a table such as > > CREATE TABLE Actions ( actiontype int, [description] varchar(20), > daysdifference int ) > > CREATE TABLE LetterActions ( id uniqueidentifier, actiontype int, actiondate > datetime ) > > INSERT INTO Actions ( actiontype , [description] , daysdifference ) > SELECT 1, 'DateCreated', 0 > UNION ALL SELECT 2, 'Stage1Date', 14 > UNION ALL SELECT 3, 'Stage2Date', 7 > UNION ALL SELECT 4, 'Stage3Date', 21 > > > DELETE FROM LetterActions > INSERT INTO LetterActions ( id, actiontype, actiondate ) > SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 1, '20050101' > UNION ALL SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 2, '20050106' > UNION ALL SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 3, '20050120' > UNION ALL SELECT '32224215-DCDD-5A27-93CA-DB80A52F1234', 1, '20050103' > UNION ALL SELECT '32224215-DCDD-5A27-93CA-DB80A52F1234', 2, '20050110' > UNION ALL SELECT '54444244-AADD-4F98-7899-DD23252F1234', 1, '20050113' > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 1, '20050102' > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 2, '20050112' > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 3, '20050118' > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 4, '20050123' > > SELECT l.id, l.ActionType AS lastAction, l.actiondate AS lastactiondate, > n.actionType AS nextaction, DATEADD(d,N.daysdifference,l.actiondate) as > NextActionDate > FROM LetterActions l > JOIN ( SELECT id, Max(Actiontype) as ActionType FROM LetterActions GROUP BY > id ) M ON L.id = M.id and L.actiontype = M.actiontype > LEFT JOIN Actions N ON N.ActionType = ( SELECT Min(ActionType) FROM Actions > A WHERE A.Actiontype > L.ActionType ) > > > John > > "JDP@Work" wrote: > > > How would I make a query to know what the next step is? > > > > Currently I run a query that returns these columns and I manually scan the data > > to look for issues. > > > > I would like to be able to have a NextAction date column, perhaps a case, but > > what would the construct look like? > > > > -- ddl > > create table #sam (id uniqueidentiifier ,recrecd datetime > > ,AppRecd datetime ,examDt datetime > > ,letter varchar(2) ,conciergedt datetime ,initltr datetime > > ,firstf datetime ,secondf datetime ,noapp datetime > > ,finalf datetime ,apprecdt datetime ,paidt datetime) > > > > /* Workflow > > > > recrecd = data entry date > > AppRecd = application received date > > examDt = exam received date > > letter = code as number or characters, which determins content of the client > > letters > > conciergedt = welcome letter sent to all new clients > > initltr = date first formal letter is sent 2 days after the concierge letter > > firstf = first Followup 14 days after initLtr > > secondf = second Followup 14 days after the firstF > > noapp = we received the exam but not the application occurs when this is true > > finalf = final followup 14 days after the second and sets initLtr = TO (timed > > out) > > apprecdt = application received date > > paidt = date payment was received > > > > An automated process fills in most of the dates when it prints the letters. */ > > > > drop #sam > > -- end snipit > > > > TIA > > > > JeffP... > > > > > > Hi
The solution would be relatively simple if you changed your design as previously indicated, this would probably help in other queries. You could also do this by creating a view CREATE VIEW LetterActions AS SELECT id , 1 as ActionType, recrecd AS actionDate FROM MyLetters WHERE recrecd IS NOT NULL UNION ALL SELECT id , 2 as ActionType, AppRecd AS actionDate FROM MyLetters WHERE AppRecd IS NOT NULL UNION ALL SELECT id , 3 as ActionType, examDt AS actionDate FROM MyLetters WHERE examDt IS NOT NULL UNION ALL SELECT id , 4 as ActionType, conciergedt AS actionDate FROM MyLetters WHERE conciergedt IS NOT NULL UNION ALL SELECT id , 5 as ActionType, initltr AS actionDate FROM MyLetters WHERE initltr IS NOT NULL UNION ALL SELECT id , 6 as ActionType, firstf AS actionDate FROM MyLetters WHERE firstf IS NOT NULL UNION ALL SELECT id , 7 as ActionType, secondf AS actionDate FROM MyLetters WHERE secondf IS NOT NULL UNION ALL SELECT id , 8 as ActionType, noapp AS actionDate FROM MyLetters WHERE noapp IS NOT NULL UNION ALL SELECT id , 9 as ActionType, finalf AS actionDate FROM MyLetters WHERE finalf IS NOT NULL UNION ALL SELECT id , 10 as ActionType, apprecdt AS actionDate FROM MyLetters WHERE apprecdt IS NOT NULL UNION ALL SELECT id , 11 as ActionType, paidt AS actionDate FROM MyLetters WHERE paidt IS NOT NULL John Show quote "JDP@Work" wrote: > John, thanks for your attempt, but I'm not sure I made myself clear. > > I don't have one lastdate/action date field, I have many. > > The more I think about it, I may write a case stmt to handle all the IIF's > > I was thinking that there was a way to eval a group of columns at once similar > to what coalesce does to return the first none null value. > > I may also implement a new field that is updated each time a workflow process is > executed > > This will be my last date and action as one varchar field. > > I can then cast the date portion back to a date and the action to a datediff > value to test against. > > This will form the basis for an exception query to find records that are not > processed in the "normal" order and to find records that have fallen off the > workflow track. > > Here's the rub, I have many more fields and events that I didn't include, so the > complexity goes & grows. > > TIA > > JeffP.... > > "John Bell" <jbellnewspo***@hotmail.com> wrote in message > news:87345044-088F-4BD3-B33C-41CCF5B06A1C@microsoft.com... > > Hi > > > > It is not clear what you are trying to achieve, but maybe thos could help! > > > > If you had a table such as > > > > CREATE TABLE Actions ( actiontype int, [description] varchar(20), > > daysdifference int ) > > > > CREATE TABLE LetterActions ( id uniqueidentifier, actiontype int, actiondate > > datetime ) > > > > INSERT INTO Actions ( actiontype , [description] , daysdifference ) > > SELECT 1, 'DateCreated', 0 > > UNION ALL SELECT 2, 'Stage1Date', 14 > > UNION ALL SELECT 3, 'Stage2Date', 7 > > UNION ALL SELECT 4, 'Stage3Date', 21 > > > > > > DELETE FROM LetterActions > > INSERT INTO LetterActions ( id, actiontype, actiondate ) > > SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 1, '20050101' > > UNION ALL SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 2, '20050106' > > UNION ALL SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 3, '20050120' > > UNION ALL SELECT '32224215-DCDD-5A27-93CA-DB80A52F1234', 1, '20050103' > > UNION ALL SELECT '32224215-DCDD-5A27-93CA-DB80A52F1234', 2, '20050110' > > UNION ALL SELECT '54444244-AADD-4F98-7899-DD23252F1234', 1, '20050113' > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 1, '20050102' > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 2, '20050112' > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 3, '20050118' > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 4, '20050123' > > > > SELECT l.id, l.ActionType AS lastAction, l.actiondate AS lastactiondate, > > n.actionType AS nextaction, DATEADD(d,N.daysdifference,l.actiondate) as > > NextActionDate > > FROM LetterActions l > > JOIN ( SELECT id, Max(Actiontype) as ActionType FROM LetterActions GROUP BY > > id ) M ON L.id = M.id and L.actiontype = M.actiontype > > LEFT JOIN Actions N ON N.ActionType = ( SELECT Min(ActionType) FROM Actions > > A WHERE A.Actiontype > L.ActionType ) > > > > > > John > > > > "JDP@Work" wrote: > > > > > How would I make a query to know what the next step is? > > > > > > Currently I run a query that returns these columns and I manually scan the > data > > > to look for issues. > > > > > > I would like to be able to have a NextAction date column, perhaps a case, > but > > > what would the construct look like? > > > > > > -- ddl > > > create table #sam (id uniqueidentiifier ,recrecd datetime > > > ,AppRecd datetime ,examDt datetime > > > ,letter varchar(2) ,conciergedt datetime ,initltr datetime > > > ,firstf datetime ,secondf datetime ,noapp datetime > > > ,finalf datetime ,apprecdt datetime ,paidt datetime) > > > > > > /* Workflow > > > > > > recrecd = data entry date > > > AppRecd = application received date > > > examDt = exam received date > > > letter = code as number or characters, which determins content of the client > > > letters > > > conciergedt = welcome letter sent to all new clients > > > initltr = date first formal letter is sent 2 days after the concierge letter > > > firstf = first Followup 14 days after initLtr > > > secondf = second Followup 14 days after the firstF > > > noapp = we received the exam but not the application occurs when this is > true > > > finalf = final followup 14 days after the second and sets initLtr = TO > (timed > > > out) > > > apprecdt = application received date > > > paidt = date payment was received > > > > > > An automated process fills in most of the dates when it prints the letters. > */ > > > > > > drop #sam > > > -- end snipit > > > > > > TIA > > > > > > JeffP... > > > > > > > > > > > > John, Got it. 1) View 2) Union All query to unifiy all the columns as one.
The only issue that I'll address in my view is the work-flow hirearchy of which date field sets the next action. For example if we have branch of the process that has 3 steps and either the case is not in that work-flow due to other condiitions or by having it completed then I need the view query to take that into account. Basically there's no easy answer, but I do see that a view is the way to go. TIA JeffP.... Show quote "John Bell" <jbellnewspo***@hotmail.com> wrote in message news:D20B8143-49E7-490C-A5F8-14F24515FE15@microsoft.com... > Hi > > The solution would be relatively simple if you changed your design as > previously indicated, this would probably help in other queries. You could > also do this by creating a view > > CREATE VIEW LetterActions AS > SELECT id , 1 as ActionType, recrecd AS actionDate > FROM MyLetters > WHERE recrecd IS NOT NULL > UNION ALL > SELECT id , 2 as ActionType, AppRecd AS actionDate > FROM MyLetters > WHERE AppRecd IS NOT NULL > UNION ALL > SELECT id , 3 as ActionType, examDt AS actionDate > FROM MyLetters > WHERE examDt IS NOT NULL > UNION ALL > SELECT id , 4 as ActionType, conciergedt AS actionDate > FROM MyLetters > WHERE conciergedt IS NOT NULL > UNION ALL > SELECT id , 5 as ActionType, initltr AS actionDate > FROM MyLetters > WHERE initltr IS NOT NULL > UNION ALL > SELECT id , 6 as ActionType, firstf AS actionDate > FROM MyLetters > WHERE firstf IS NOT NULL > UNION ALL > SELECT id , 7 as ActionType, secondf AS actionDate > FROM MyLetters > WHERE secondf IS NOT NULL > UNION ALL > SELECT id , 8 as ActionType, noapp AS actionDate > FROM MyLetters > WHERE noapp IS NOT NULL > UNION ALL > SELECT id , 9 as ActionType, finalf AS actionDate > FROM MyLetters > WHERE finalf IS NOT NULL > UNION ALL > SELECT id , 10 as ActionType, apprecdt AS actionDate > FROM MyLetters > WHERE apprecdt IS NOT NULL > UNION ALL > SELECT id , 11 as ActionType, paidt AS actionDate > FROM MyLetters > WHERE paidt IS NOT NULL > > John > > "JDP@Work" wrote: > > > John, thanks for your attempt, but I'm not sure I made myself clear. > > > > I don't have one lastdate/action date field, I have many. > > > > The more I think about it, I may write a case stmt to handle all the IIF's > > > > I was thinking that there was a way to eval a group of columns at once similar > > to what coalesce does to return the first none null value. > > > > I may also implement a new field that is updated each time a workflow process is > > executed > > > > This will be my last date and action as one varchar field. > > > > I can then cast the date portion back to a date and the action to a datediff > > value to test against. > > > > This will form the basis for an exception query to find records that are not > > processed in the "normal" order and to find records that have fallen off the > > workflow track. > > > > Here's the rub, I have many more fields and events that I didn't include, so the > > complexity goes & grows. > > > > TIA > > > > JeffP.... > > > > "John Bell" <jbellnewspo***@hotmail.com> wrote in message > > news:87345044-088F-4BD3-B33C-41CCF5B06A1C@microsoft.com... > > > Hi > > > > > > It is not clear what you are trying to achieve, but maybe thos could help! > > > > > > If you had a table such as > > > > > > CREATE TABLE Actions ( actiontype int, [description] varchar(20), > > > daysdifference int ) > > > > > > CREATE TABLE LetterActions ( id uniqueidentifier, actiontype int, actiondate > > > datetime ) > > > > > > INSERT INTO Actions ( actiontype , [description] , daysdifference ) > > > SELECT 1, 'DateCreated', 0 > > > UNION ALL SELECT 2, 'Stage1Date', 14 > > > UNION ALL SELECT 3, 'Stage2Date', 7 > > > UNION ALL SELECT 4, 'Stage3Date', 21 > > > > > > > > > DELETE FROM LetterActions > > > INSERT INTO LetterActions ( id, actiontype, actiondate ) > > > SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 1, '20050101' > > > UNION ALL SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 2, '20050106' > > > UNION ALL SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 3, '20050120' > > > UNION ALL SELECT '32224215-DCDD-5A27-93CA-DB80A52F1234', 1, '20050103' > > > UNION ALL SELECT '32224215-DCDD-5A27-93CA-DB80A52F1234', 2, '20050110' > > > UNION ALL SELECT '54444244-AADD-4F98-7899-DD23252F1234', 1, '20050113' > > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 1, '20050102' > > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 2, '20050112' > > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 3, '20050118' > > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 4, '20050123' > > > > > > SELECT l.id, l.ActionType AS lastAction, l.actiondate AS lastactiondate, > > > n.actionType AS nextaction, DATEADD(d,N.daysdifference,l.actiondate) as > > > NextActionDate > > > FROM LetterActions l > > > JOIN ( SELECT id, Max(Actiontype) as ActionType FROM LetterActions GROUP BY > > > id ) M ON L.id = M.id and L.actiontype = M.actiontype > > > LEFT JOIN Actions N ON N.ActionType = ( SELECT Min(ActionType) FROM Actions > > > A WHERE A.Actiontype > L.ActionType ) > > > > > > > > > John > > > > > > "JDP@Work" wrote: > > > > > > > How would I make a query to know what the next step is? > > > > > > > > Currently I run a query that returns these columns and I manually scan the > > data > > > > to look for issues. > > > > > > > > I would like to be able to have a NextAction date column, perhaps a case, > > but > > > > what would the construct look like? > > > > > > > > -- ddl > > > > create table #sam (id uniqueidentiifier ,recrecd datetime > > > > ,AppRecd datetime ,examDt datetime > > > > ,letter varchar(2) ,conciergedt datetime ,initltr datetime > > > > ,firstf datetime ,secondf datetime ,noapp datetime > > > > ,finalf datetime ,apprecdt datetime ,paidt datetime) > > > > > > > > /* Workflow > > > > > > > > recrecd = data entry date > > > > AppRecd = application received date > > > > examDt = exam received date > > > > letter = code as number or characters, which determins content of the client > > > > letters > > > > conciergedt = welcome letter sent to all new clients > > > > initltr = date first formal letter is sent 2 days after the concierge letter > > > > firstf = first Followup 14 days after initLtr > > > > secondf = second Followup 14 days after the firstF > > > > noapp = we received the exam but not the application occurs when this is > > true > > > > finalf = final followup 14 days after the second and sets initLtr = TO > > (timed > > > > out) > > > > apprecdt = application received date > > > > paidt = date payment was received > > > > > > > > An automated process fills in most of the dates when it prints the letters. > > */ > > > > > > > > drop #sam > > > > -- end snipit > > > > > > > > TIA > > > > > > > > JeffP... > > > > > > > > > > > > > > > > > > Hi
A couple more thoughts but I can't say if they will be any use!!! You may want to allow gaps between the numbering of your actions so you can insert a new highest action. There may also be a need a second identifier e.g. threadid so the next step is the maximum of that thread, once the thread's steps are exhausted you go back to the previous thread. John Show quote "JDP@Work" wrote: > John, Got it. 1) View 2) Union All query to unifiy all the columns as one. > > The only issue that I'll address in my view is the work-flow hirearchy of which > date field sets the next action. > > For example if we have branch of the process that has 3 steps and either the > case is not in that work-flow due to other condiitions or by having it completed > then I need the view query to take that into account. > > Basically there's no easy answer, but I do see that a view is the way to go. > > TIA > > JeffP.... > > "John Bell" <jbellnewspo***@hotmail.com> wrote in message > news:D20B8143-49E7-490C-A5F8-14F24515FE15@microsoft.com... > > Hi > > > > The solution would be relatively simple if you changed your design as > > previously indicated, this would probably help in other queries. You could > > also do this by creating a view > > > > CREATE VIEW LetterActions AS > > SELECT id , 1 as ActionType, recrecd AS actionDate > > FROM MyLetters > > WHERE recrecd IS NOT NULL > > UNION ALL > > SELECT id , 2 as ActionType, AppRecd AS actionDate > > FROM MyLetters > > WHERE AppRecd IS NOT NULL > > UNION ALL > > SELECT id , 3 as ActionType, examDt AS actionDate > > FROM MyLetters > > WHERE examDt IS NOT NULL > > UNION ALL > > SELECT id , 4 as ActionType, conciergedt AS actionDate > > FROM MyLetters > > WHERE conciergedt IS NOT NULL > > UNION ALL > > SELECT id , 5 as ActionType, initltr AS actionDate > > FROM MyLetters > > WHERE initltr IS NOT NULL > > UNION ALL > > SELECT id , 6 as ActionType, firstf AS actionDate > > FROM MyLetters > > WHERE firstf IS NOT NULL > > UNION ALL > > SELECT id , 7 as ActionType, secondf AS actionDate > > FROM MyLetters > > WHERE secondf IS NOT NULL > > UNION ALL > > SELECT id , 8 as ActionType, noapp AS actionDate > > FROM MyLetters > > WHERE noapp IS NOT NULL > > UNION ALL > > SELECT id , 9 as ActionType, finalf AS actionDate > > FROM MyLetters > > WHERE finalf IS NOT NULL > > UNION ALL > > SELECT id , 10 as ActionType, apprecdt AS actionDate > > FROM MyLetters > > WHERE apprecdt IS NOT NULL > > UNION ALL > > SELECT id , 11 as ActionType, paidt AS actionDate > > FROM MyLetters > > WHERE paidt IS NOT NULL > > > > John > > > > "JDP@Work" wrote: > > > > > John, thanks for your attempt, but I'm not sure I made myself clear. > > > > > > I don't have one lastdate/action date field, I have many. > > > > > > The more I think about it, I may write a case stmt to handle all the IIF's > > > > > > I was thinking that there was a way to eval a group of columns at once > similar > > > to what coalesce does to return the first none null value. > > > > > > I may also implement a new field that is updated each time a workflow > process is > > > executed > > > > > > This will be my last date and action as one varchar field. > > > > > > I can then cast the date portion back to a date and the action to a datediff > > > value to test against. > > > > > > This will form the basis for an exception query to find records that are not > > > processed in the "normal" order and to find records that have fallen off the > > > workflow track. > > > > > > Here's the rub, I have many more fields and events that I didn't include, so > the > > > complexity goes & grows. > > > > > > TIA > > > > > > JeffP.... > > > > > > "John Bell" <jbellnewspo***@hotmail.com> wrote in message > > > news:87345044-088F-4BD3-B33C-41CCF5B06A1C@microsoft.com... > > > > Hi > > > > > > > > It is not clear what you are trying to achieve, but maybe thos could help! > > > > > > > > If you had a table such as > > > > > > > > CREATE TABLE Actions ( actiontype int, [description] varchar(20), > > > > daysdifference int ) > > > > > > > > CREATE TABLE LetterActions ( id uniqueidentifier, actiontype int, > actiondate > > > > datetime ) > > > > > > > > INSERT INTO Actions ( actiontype , [description] , daysdifference ) > > > > SELECT 1, 'DateCreated', 0 > > > > UNION ALL SELECT 2, 'Stage1Date', 14 > > > > UNION ALL SELECT 3, 'Stage2Date', 7 > > > > UNION ALL SELECT 4, 'Stage3Date', 21 > > > > > > > > > > > > DELETE FROM LetterActions > > > > INSERT INTO LetterActions ( id, actiontype, actiondate ) > > > > SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 1, '20050101' > > > > UNION ALL SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 2, '20050106' > > > > UNION ALL SELECT '76664215-AFDD-4F07-92CB-EA70E03E6541', 3, '20050120' > > > > UNION ALL SELECT '32224215-DCDD-5A27-93CA-DB80A52F1234', 1, '20050103' > > > > UNION ALL SELECT '32224215-DCDD-5A27-93CA-DB80A52F1234', 2, '20050110' > > > > UNION ALL SELECT '54444244-AADD-4F98-7899-DD23252F1234', 1, '20050113' > > > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 1, '20050102' > > > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 2, '20050112' > > > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 3, '20050118' > > > > UNION ALL SELECT '22345243-DDAA-9088-2313-552FAB768763', 4, '20050123' > > > > > > > > SELECT l.id, l.ActionType AS lastAction, l.actiondate AS lastactiondate, > > > > n.actionType AS nextaction, DATEADD(d,N.daysdifference,l.actiondate) as > > > > NextActionDate > > > > FROM LetterActions l > > > > JOIN ( SELECT id, Max(Actiontype) as ActionType FROM LetterActions GROUP > BY > > > > id ) M ON L.id = M.id and L.actiontype = M.actiontype > > > > LEFT JOIN Actions N ON N.ActionType = ( SELECT Min(ActionType) FROM > Actions > > > > A WHERE A.Actiontype > L.ActionType ) > > > > > > > > > > > > John > > > > > > > > "JDP@Work" wrote: > > > > > > > > > How would I make a query to know what the next step is? > > > > > > > > > > Currently I run a query that returns these columns and I manually scan > the > > > data > > > > > to look for issues. > > > > > > > > > > I would like to be able to have a NextAction date column, perhaps a > case, > > > but > > > > > what would the construct look like? > > > > > > > > > > -- ddl > > > > > create table #sam (id uniqueidentiifier ,recrecd datetime > > > > > ,AppRecd datetime ,examDt datetime > > > > > ,letter varchar(2) ,conciergedt datetime ,initltr datetime > > > > > ,firstf datetime ,secondf datetime ,noapp datetime > > > > > ,finalf datetime ,apprecdt datetime ,paidt datetime) > > > > > > > > > > /* Workflow > > > > > > > > > > recrecd = data entry date > > > > > AppRecd = application received date > > > > > examDt = exam received date > > > > > letter = code as number or characters, which determins content of the > client > > > > > letters > > > > > conciergedt = welcome letter sent to all new clients > > > > > initltr = date first formal letter is sent 2 days after the concierge > letter > > > > > firstf = first Followup 14 days after initLtr > > > > > secondf = second Followup 14 days after the firstF > > > > > noapp = we received the exam but not the application occurs when this is > > > true > > > > > finalf = final followup 14 days after the second and sets initLtr = TO > > > (timed > > > > > out) > > > > > apprecdt = application received date > > > > > paidt = date payment was received > > > > > > > > > > An automated process fills in most of the dates when it prints the > letters. > > > */ > > > > > > > > > > drop #sam > > > > > -- end snipit > > > > > > > > > > TIA > > > > > > > > > > JeffP... > > > > > > > > > > > > > > > > > > > > > > > > > > > |
|||||||||||||||||||||||