SQL Server根据计算从多行中选择一个

时间:2017-02-13 14:41:24

标签: sql-server-2008-r2

我试图找到最多的天数(以及审稿人的徽章编号),其中一位审稿人批准了工作流程中的特定文档。例如,我有一个表,其中包含多个工作流程批准步骤(提交者,经理,控制者,QA),以及他们的徽章编号和他们批准的日期。该表被称为" Workflow"并将上面提到的这四个工作流程步骤作为表中的记录,以及与工作流程具有一对多关系的主表格设计。

我试图确定最长审核步骤(天数)的天数,以及该步骤的审核者的徽章编号(基本上是谁阻止了审批工作流程)。我一直试图设置自己的变量以便以后使用,但不知道如何设置徽章编号,我感到困惑。我已经尝试了CASE,IIF和COALESCE,但我没有运气,因为我不想要返回第一个真值,然后停止,我希望它继续评估所有步骤。以下是我的SQL示例:

   declare @managerTime int = 0
    declare @controllerTime int = 0
    declare @qaTime int = 0

    SET @managerTime = (SELECT DATEDIFF(day, manager.BadgeDate, submitter.BadgeDate)
    from Design d
    left outer join Workflow submitter on (d.DCRId = submitter.DCRId and submitter.RoleName = 'Submitter')
    left outer join Workflow manager on (d.DCRId = manager.DCRId and manager.RoleName = 'System Manager')

    SET @controllerTime = (SELECT DATEDIFF(day, controller.BadgeDate, manager.BadgeDate)
        from Design d
        left outer join Workflow manager on (d.DCRId = manager.DCRId and manager.RoleName = 'System Manager')
        left outer join Workflow controller on (d.DCRId = controller.DCRId and controller.RoleName = 'DCR Controller')

2 个答案:

答案 0 :(得分:0)

我就是这样做的:

使用流程定义创建表WorkflowDefinition:

Source          Destination     Description
Submitter       System Manager  Submitter -> System Manager
System Manager  DCR Controller  System Manager -> DCR Controller
DCR Controller  QA              DCR Controller -> QA

现在我们使用此表来连接工作流元素并计算最多天数:

SET @MaxTime = (SELECT MAX(DATEDIFF(day, source.BadgeDate, destination.BadgeDate))
    from Design d
    inner join Workflow source on d.DCRId = source.DCRId 
    inner join WorkflowDefinition flow on source.RoleName = flow.source
    inner join Workflow destination on d.DCRId = destination.DCRId 
    and destination.RoleName = flow.destination
)

当我们有这个值时,我们可以选择完成这个确切天数的所有工作流程元素:

Select 
    destination.BadgeNumber
from Design d
inner join Workflow source on d.DCRId = source.DCRId 
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
where DATEDIFF(day, source.BadgeDate, destination.BadgeDate) = @MaxTime 

如果您想分别知道每种类型步骤的最大天数,那么我们可以这样做:计算每步类型的最大天数并将其放入临时表:

SELECT 
    MAX(DATEDIFF(day, source.BadgeDate, destination.BadgeDate)) as maxDays,
    flow.Description as StepDescription
into #tmp
from Design d
inner join Workflow source on d.DCRId = source.DCRId 
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId 
and destination.RoleName = flow.destination
group by flow.Description

现在使用此表选择与最大天数和步骤描述匹配的步骤:

Select 
    destination.BadgeNumber
from Design d
inner join Workflow source on d.DCRId = source.DCRId 
inner join WorkflowDefinition flow on source.RoleName = flow.source
inner join Workflow destination on d.DCRId = destination.DCRId
and destination.RoleName = flow.destination
inner join #tmp on DATEDIFF(day, source.BadgeDate, destination.BadgeDate) = maxDays and StepDescription = flow.Description

答案 1 :(得分:0)

我最终在"很多" table(工作流),而不是Design表。我使用了以下SQL,它让我获得了我追求的结果。谢谢大家试图弄清楚我的谣言。

select
w.DesignId,
w.RoleName,
w.BadgeNumber,
w.BadgeDate,
DATEDIFF(day, 
    (select x.BadgeDate from Workflow x 
        where x.BadgeDate is not null
        and x.DesignId = w.DesignId 
        and x.StepOrder = w.StepOrder - 1),      
    (select b.BadgeDate from Workflow b 
        where b.BadgeDate is not null
        and b.DesignId = w.DesignId 
        and b.StepOrder = w.StepOrder))
 as StepDuration,
w.StepOrder,
TotalDuration = DATEDIFF(day, 
    (select y.BadgeDate from Workflow y 
        where y.RoleName = 'Submitter'
        and y.DesignId = w.DesignId),       
    (select v.BadgeDate from Workflow v 
        where v.RoleName = 'Approver'
        and v.DesignId = w.DesignId)),
d.VersionNumber,
d.Title 
from Workflow w
inner join Design d on d.DesignId = w.DesignId