-1
select TRANSACTION, comp_time, start_time, (comp_time - start_time) as DIFF from table
where start_time = trunc(sysdate) 

我想从 DIFF 值计算,在同一个 sql 语句中,有多少 TRANSACTION 在不到 10 秒、1 分钟和 15 分钟内完成。我对 SQL 很陌生.. 可能我们可以使用一些 if-else 或一些 CASE 条件.. 在获得每个值的计数之后,我需要计算每个值的百分比..

假设我总共有 3000 笔交易,其中 1500 笔交易在不到 10 秒内完成。因此,1 分钟和 15 分钟的百分比将是 50%。

4

2 回答 2

2

标准化结果 Oracle 版本,使用分析函数计算百分比。

with cte_durations as (
  select
    case when (comp_time - start_time) * 24 * 60 * 60 < 10 then '< 10 sec'
         when (comp_time - start_time) * 24 * 60 * 60 < 60 then '< 1 min'
         when (comp_time - start_time) * 24 * 60      < 15 then '< 15 min'
         else                                                   '>= 15 min'
    end duration_bucket
  from  table
  where    start_time = trunc(sysdate))
select   duration_bucket,
         count(*),
         100*ratio_to_report(count(*)) over () pct_of_total
from     cte_durations
group by duration_bucket
order by case duration_bucket         
           when '< 10 sec'  then 1
           when '< 1 min'   then 2
           when '< 15 min'  then 3
           when '>= 15 min' then 4
         end
/
于 2013-10-03T11:41:35.687 回答
1

对于 SQL 服务器:

with timecounts as (
    select count(case when datediff(ss, start_time, comp_time) <= 10 then transactionId end) as q10s,
           count(case when datediff(ss, start_time, comp_time) <= 60 then transactionId end) as q1m,
           count(case when datediff(mi, start_time, comp_time) <= 15 then transactionId end) as q15m,
           count(*) qTotal   
    from   table
    where  start_time = trunc(sysdate)
    )

select q10s,
       q1m,
       q15m,
       case when qTotal <> 0 then cast(q10s as float) / qTotal end as q10sPerc,
       case when qTotal <> 0 then cast(q1m as float) / qTotal end as q1mPerc,
       case when qTotal <> 0 then cast(q15m as float) / qTotal end as q15mPerc
from   TimeCounts

对于甲骨文:

with timecounts as (
    select count(case when (comp_time - start_time) * 86400 <= 10 then transactionId end) as q10s,
           count(case when (comp_time - start_time) * 86400 <= 60 then transactionId end) as q1m,
           count(case when (comp_time - start_time) * 1440 <= 15 then transactionId end) as q15m,
           count(*) qTotal   
    from   myTable
    where  start_time = trunc(sysdate)
    )

select q10s,
       q1m,
       q15m,
       case when qTotal <> 0 then cast(q10s as float) / qTotal end as q10sPerc,
       case when qTotal <> 0 then cast(q1m as float) / qTotal end as q1mPerc,
       case when qTotal <> 0 then cast(q15m as float) / qTotal end as q15mPerc
from   TimeCounts
于 2013-10-02T12:33:49.670 回答