我正在使用SQL报告构建器并希望在周转时间内计算%
我的表看起来像
Name count within tat
jeff 1 1
jeff 1 0
jeff 1 1
jeff 1 0
我希望它看起来像这样。
Name count within tat
jeff 4 2 (50%)
用于在内部计算的代码是
case
when (convert(Decimal(10,2),
(cast(datediff(minute,
(CAST(RequestDate AS DATETIME) + CAST(RequestTime AS DATETIME)),
REQUEST_TESTLINES.AuthDateTime)as float)/60/24))) >
EXP_TAT then '1' else '0' end as [withintat]
我如何总结这一栏?
答案 0 :(得分:0)
您可以将其换成另一个SELECT
。
SELECT SUM(count), SUM(withintat)
FROM (/*Your original query*/)
答案 1 :(得分:0)
是的,您可以在sum()
中使用案例陈述但它需要返回一个数字..
你内在的变化"在#34;像
这样的东西select case when (convert(Decimal(10,2),
(cast(datediff(minute,
(CAST(RequestDate AS DATETIME) + CAST(RequestTime AS DATETIME)),
REQUEST_TESTLINES.AuthDateTime)as float)/60/24))) >
EXP_TAT
then 1
else 0 end as [withintat]
....
但是,如果你需要总和和百分比。
你需要两次使用这个值。
我相信你不想保留复制代码。
所以使用您的实际查询作为子查询来总结它可能是一个好主意......
如果你真的不想将它用作子查询
你应该使用外部应用来收集withintat的值,做这样的事情
select Name
,count(*) as [count]
,sum(OA.[withintat]) as [withintat]
,sum(OA.[withintat])*100/count(*) as [percent]
,cast(sum(OA.[withintat]) as varchar(max))+' ('+cast(sum(OA.[withintat])*100/count(*) as varchar(max))+' %)' as [withintat_and_percent]
from your_actual_query
outer apply(select case when (convert(Decimal(10,2),
(cast(datediff(minute,
(CAST(RequestDate AS DATETIME) + CAST(RequestTime AS DATETIME)),
REQUEST_TESTLINES.AuthDateTime)as float)/60/24))) > EXP_TAT
then 1
else 0 end as [withintat]
)OA
where ....
答案 2 :(得分:0)
在这种情况下我会使用IF
(旁观)。我试图降低比较的复杂性,但没有看到一些实际数据,这是最好的猜测。
select
name,
count(name) as count,
concat(
sum(if(datediff(minute,
(cast(RequestDate AS DATETIME) +
cast(RequestTime AS DATETIME)),
REQUEST_TESTLINES.AuthDateTime) / 60 / 24 > EXP_TAT, 1, 0 )),
' (',
format(
(sum(if(datediff(minute,
(cast(RequestDate AS DATETIME) +
cast(RequestTime AS DATETIME)),
REQUEST_TESTLINES.AuthDateTime) / 60 / 24 > EXP_TAT, 1, 0 )
)/count(name))*100,1),
'%)'
) as 'within tat'