繁体   English   中英

如何根据两个不同的 case 语句获得 Datediff 计算?

[英]How to get a Datediff calculation based on two different case statements?

我有一个很长的选择查询,但已在此处粘贴了相关部分。

我需要弄清楚如何获取 OrderSignedDate 和 OrderIssuedDate 之间的 datediff,它们分别位于我的选择查询中的单独 case 语句中。 任何帮助将不胜感激:

select tkh.task_id
,cas.case_number as CaseNo
,cst.description as CaseStatus
,cas.title as Title
,ztt.description as OrderType
,tkh.task_status

--Order Signed Date
,(select (case when 
zrt.description = 'Administrative Law Judge'
and tka.completed_date is not null
then convert(varchar(10), tka.completed_date, 101) END) 
from ecl_task_actor tka
join ecl_resource res on tka.entity_pk_id = res.resource_id
join ecl_z_resource_type zrt on res.resource_type_id = zrt.resource_type_id
where tka.task_id = tkh.task_id
and tka.task_actor_status <> 'D'
and zrt.description = 'Administrative law judge') as OrderSignedDate

--Date Order was Issued
,(select (case when 
zrt.description = 'Order Processor Team'
and tka.completed_date is not null
then convert(varchar(10), tka.completed_date,101)
END) 
from ecl_task_actor tka
join ecl_resource res on tka.entity_pk_id = res.resource_id
join ecl_z_resource_type zrt on res.resource_type_id = zrt.resource_type_id
where tka.task_id = tkh.task_id
and tka.task_actor_status <> 'D'
and zrt.description = 'Order processor Team') as OrderIssuedDate


from ecl_task_header tkh
join ecl_case cas on tkh.case_id = cas.case_id
JOIN ecl_z_case_status_type as cst on cst.case_status_type_id = cas.case_status_type_id
join ecl_z_task_type ztt on tkh.task_type_id = ztt.task_type_id

where ztt.description like '%Issue%'
order by cas.case_number asc

其实一点也不难。 您的选择如下所示:

select ...,
(...) as OrderSignedDate,
(...) as OrderIssueDate,
...

... 表示某些东西在那里保持不变。 现在,让我们将其用作table

select ..., OrderSignedDate, OrderIssueDate, ...,
from (
select ...,
(...) as OrderSignedDate,
(...) as OrderIssueDate,
...
) t

而在外部select这是你的周围包裹的初始select ,您可以使用OrderSignedDateOrderIssueDate为列,你可以datediff他们。

我假设您希望将此内联添加到现有查询中。

由于定义两个现有日期的子选择通过它们的WHERE子句与外部查询相关联,因此没有一种很好的方法可以避免重复代码。 另一方面,这意味着它就像重复代码一样简单。 使用现有的子选择作为DATEDIFF的参数。

这应该有效:

...<The current SELECT list, plus>
  ,DATEDIFF
  (
    DAY,
    (--Order Signed Date
    SELECT (CASE
              WHEN zrt.description = 'Administrative Law Judge'
                   AND tka.completed_date IS NOT NULL
                THEN CONVERT(varchar(10), tka.completed_date, 101)
            END
           )
    FROM
      ecl_task_actor AS tka
    JOIN
      ecl_resource AS res
        ON
        tka.entity_pk_id = res.resource_id
    JOIN
      ecl_z_resource_type AS zrt
        ON
        res.resource_type_id = zrt.resource_type_id
    WHERE
      tka.task_id = tkh.task_id
      AND tka.task_actor_status <> 'D'
      AND zrt.description = 'Administrative law judge'
    ),
   (--Date Order was Issued
    SELECT (CASE
              WHEN zrt.description = 'Order Processor Team'
                   AND tka.completed_date IS NOT NULL
                THEN CONVERT(varchar(10), tka.completed_date, 101)
            END
           )
    FROM
      ecl_task_actor AS tka
    JOIN
      ecl_resource AS res
        ON
        tka.entity_pk_id = res.resource_id
    JOIN
      ecl_z_resource_type AS zrt
        ON
        res.resource_type_id = zrt.resource_type_id
    WHERE
      tka.task_id = tkh.task_id
      AND tka.task_actor_status <> 'D'
      AND zrt.description = 'Order processor Team'
   )
  ) AS DaysBetween
... <The rest of the query>

编辑:这应该有效,因为这两个子选择都解析为每行数据的日期,所以我们只是为DATEDIFF函数重用这两个日期。 它会增加一点开销,但我的猜测是,对于已经如此庞大的查询,瞬间性能不一定是主要问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM