同级别计数结果的总和



我正在尝试对同一级别的count(id(的结果求和,以便从总计数中找出count(id(的相对部分。

计数是按各自以前的数字分组的,我想呆在同一张桌子上,把它们放在一起。

`

select totalattempts, count(totalattempts) allattempts, count(case when success>0 then totalattempts else null end) successfulattempts
from ( 

select *, case when success> 0 then attemptspresuccess+1 else attemptspresuccess end totalattempts
from (select orderid, count(orderid) attemptspresuccess, count(case when recoveredPaymentId is not null then recoveredPaymentId end ) success from (
select orderid, recoveredPaymentId
from errors
where platform = 'woo'
) alitable
group by orderid) minitable ) finaltable
group by totalattempts
order by totalattempts asc

`

我需要添加另一列,简单地说,它基本上有count(totaltempts(/sum(count(totalempts(。

我基本上没有什么想法了。

我不能使用windows,因为这是一个不支持的retool应用程序

假设这里有一些测试数据:

DECLARE @table TABLE (AttemptNumber INT IDENTITY, Success BIT)
INSERT INTO @table (Success) VALUES
(0),(0),(0),(0),(1),(1),(0),(0),(0),(0),(0),(1),(0),(1),(0),(0),
(0),(0),(1),(0),(0),(0),(0),(1),(0),(1),(0),(0),(0),(1),(0),(0)

听起来你想知道有多少次尝试,有多少次成功,这是一个百分比?

SELECT COUNT(Success) AS TotalCount, 
COUNT(CASE WHEN Success = 1 THEN 1 END) AS SuccessCount,
COUNT(CASE WHEN Success = 1.0 THEN 1 END)/(COUNT(Success)+.0) AS SuccessPct
FROM @table
TotalCount  SuccessCount    SuccessPct
--------------------------------------
32          8               0.2500000000000

最新更新