SQL:查找 MIN 但大于 MAX

SQL: Find MIN but greater than a MAX

我正在处理任务历史并试图找到附加到同一条记录的两个日期:1) 最近批准任务的时间(最大批准); 2) 批准后的首次提交日期。

这是我目前的情况:

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

这很接近,但是,这是我第一次提交任务。我确定我需要使用另一个子查询,我只是不知道如何在同一结果中引用列。

这是任务历史记录。突出显示的是我要显示的两个日期:

我不知道我能否在任何合理的时间内完成您的查询,但要获取特定行之后的行,您需要执行如下操作:

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

通常,使用 MAX() 获取最后批准日期,然后使用 MIN() 获取该日期之后的第一个日期,其中 DateAdded > 该最大值,然后 select 该日期的行。我添加了 Top 1,以防万一当时恰好有多个行。不确定您的数据是否可行,但为了安全起见。

在一些帮助下,我们发现我们需要额外的时间围绕查询。

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