0

I am working with task history and trying to find two dates attached to the same record: 1) Most recent time a task was approved (max approve); 2)The first submitted date after said approval.

Here is what I have so far:

Select  
a.assn_uid,
max(b.ASSN_TRANS_DATE_ENTERED) as LastApprove, 
e.LastSubmitted 

FROM [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS] a
inner join [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS_COMMENTS] b
on a.ASSN_TRANS_UID = b.ASSN_TRANS_UID



join (select c.assn_uid,
min(d.ASSN_TRANS_DATE_ENTERED) as LastSubmitted

FROM [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS] c
inner join [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS_COMMENTS] d
on c.ASSN_TRANS_UID = d.ASSN_TRANS_UID

where c.ASSN_UID = '499879BC-28B2-E411-8B0A-00059A3C7A00'
and d.[ASSN_TRANS_COMMENT_TYPE_ENUM] = 0

group by c.assn_uid ) e
on e.ASSN_UID = a.ASSN_UID


where a.ASSN_UID = '499879BC-28B2-E411-8B0A-00059A3C7A00'
and b.[ASSN_TRANS_COMMENT_TYPE_ENUM] = 1

group by a.assn_uid, e.LastSubmitted

This is close, however, it gives me the first time ever that the task was submitted. I am sure that I need to use another subquery, I just dont know how to reference a column within the same result.

Here is the task history. Highlighted are the two dates I am trying to show: enter image description here

abauman
  • 96
  • 6

2 Answers2

0

I don't know that I could wade through your query in any reasonable amount of time, but to get the row after a particular row, you'd need to do something like this:

create table #submissions (
        ID int,
        DateAdded datetime,
        SubmissionType nvarchar(100)
    )
insert #submissions values
    (1, '2010-01-01', 'first ever'),
    (1, '2010-01-02', 'second'),
    (1, '2010-01-03', 'third'),
    (1, '2010-01-04', 'approve'),
    (1, '2010-01-05', 'first after approve'),
    (1, '2010-01-06', 'second after approve'),
    (1, '2010-01-07', 'third after approve')

declare @lastApprovalDate datetime

select @lastApprovalDate = MAX(DateAdded)
    from #submissions
    where
        SubmissionType = 'approve'

declare @firstAfterApprovalDate datetime
select @firstAfterApprovalDate = MIN(DateAdded)
    from #submissions
    where
        DateAdded > @lastApprovalDate

select *
    from #submissions
    where
        DateAdded = @firstAfterApprovalDate

drop table #submissions

In general, get the last approval date using MAX(), then get the first date after that date using MIN() where DateAdded > that max, and then select the row at that date. I added Top 1, just in case there happen to be multiple rows at that time. Not sure if that's possible in your data, but just to be safe.

Chris Steele
  • 1,343
  • 1
  • 9
  • 20
0

With some help, we figured out we needed an additional min wrapped around the query.

SELECT
       final.assn_uid,
       final.LastApprove,
       min(final.SubmissionDate) FirstSubmitted
FROM
       (Select  
       a.assn_uid,
       max(b.ASSN_TRANS_DATE_ENTERED) as LastApprove, 
       e.SubmittedDates SubmissionDate

       FROM [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS] a
       inner join [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS_COMMENTS] b
       on a.ASSN_TRANS_UID = b.ASSN_TRANS_UID



       join (select c.assn_uid,
       (d.ASSN_TRANS_DATE_ENTERED) as SubmittedDates

       FROM [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS] c
       inner join [PRJDEV_ProjectWebApp].[pub].[MSP_ASSIGNMENT_TRANSACTIONS_COMMENTS] d
       on c.ASSN_TRANS_UID = d.ASSN_TRANS_UID

       where c.ASSN_UID = '499879BC-28B2-E411-8B0A-00059A3C7A00'
       and d.[ASSN_TRANS_COMMENT_TYPE_ENUM] = 0

       ) e
       on e.ASSN_UID = a.ASSN_UID


       where a.ASSN_UID = '499879BC-28B2-E411-8B0A-00059A3C7A00'
       and b.[ASSN_TRANS_COMMENT_TYPE_ENUM] = 1
       and e.SubmittedDates > b.ASSN_TRANS_DATE_ENTERED

       group by a.assn_uid, e.SubmittedDates) Final

GROUP BY
       final.assn_uid,
       final.LastApprove
abauman
  • 96
  • 6