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. 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
:
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.
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. 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
.
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.