[英]How to get a Datediff calculation based on two different case statements?
I have a very long select query but have pasted the relevant pieces here.我有一个很长的选择查询,但已在此处粘贴了相关部分。
I need to figure out how to get the datediff between the OrderSignedDate and the OrderIssuedDate that are each in a separate case statement within my select query.我需要弄清楚如何获取 OrderSignedDate 和 OrderIssuedDate 之间的 datediff,它们分别位于我的选择查询中的单独 case 语句中。 Any help would be greatly appreciated:任何帮助将不胜感激:
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
It's actually not difficult at all.其实一点也不难。 Your selection looks like this:您的选择如下所示:
select ...,
(...) as OrderSignedDate,
(...) as OrderIssueDate,
...
The ... denotes that some stuff is there which remains unchanged. ... 表示某些东西在那里保持不变。 Now, let's use this as a table
:现在,让我们将其用作table
:
select ..., OrderSignedDate, OrderIssueDate, ...,
from (
select ...,
(...) as OrderSignedDate,
(...) as OrderIssueDate,
...
) t
and at the outer select
which is wrapped around your initial select
you can use OrderSignedDate
and OrderIssueDate
as columns and you can datediff
them.而在外部select
这是你的周围包裹的初始select
,您可以使用OrderSignedDate
和OrderIssueDate
为列,你可以datediff
他们。
I'm assuming you're looking to add this inline to your existing query.我假设您希望将此内联添加到现有查询中。
Since the sub-selects that define the two existing dates are correlated to the outer query through their WHERE
clauses, there's not a great way to avoid repeating the code.由于定义两个现有日期的子选择通过它们的WHERE
子句与外部查询相关联,因此没有一种很好的方法可以避免重复代码。 On the other hand, that means it's as simple as just repeating the code.另一方面,这意味着它就像重复代码一样简单。 Use the existing sub-selects as the parameters for your DATEDIFF
.使用现有的子选择作为DATEDIFF
的参数。
This ought to work:这应该有效:
...<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>
Edit: This should work because those two sub-selects both resolve into dates for each row of data, so we're just reusing those two dates for the DATEDIFF
function.编辑:这应该有效,因为这两个子选择都解析为每行数据的日期,所以我们只是为DATEDIFF
函数重用这两个日期。 It'll add a little overhead, but my guess is that with a query that's already this big, split-second performance isn't necessarily the primary concern.它会增加一点开销,但我的猜测是,对于已经如此庞大的查询,瞬间性能不一定是主要问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.