按子计数除以计数(*)

时间:2015-12-10 00:18:48

标签: sql oracle

我正在运行一个查询并试图获得一个百分比,以便在数据库中获得失败与成功。

{success:false, error: err}

它返回错误'ORA-00937:不是单组组功能'。我原以为子查询会返回一个值,所以我只能将它除以count(*)。 Aby的想法?

3 个答案:

答案 0 :(得分:3)

你的查询过于复杂。您不需要任何子查询:

select count(case when response != 'VERIFIED' then 'X' end) / count(*) as percentage
  from validation
 where request_date BETWEEN (SYSDATE - 30/(24*60)) AND SYSDATE

答案 1 :(得分:1)

可能你只需要2个双子查询子?

 Select 
   (SELECT count(*) FROM validation WHERE request_date BETWEEN (SYSDATE - 30/(24*60)) AND SYSDATE AND response != 'VERIFIED')/
   (SELECT count(*) FROM validation WHERE request_date BETWEEN (SYSDATE - 30/(24*60)) AND SYSDATE)
 from dual;

答案 2 :(得分:1)

Personally, I think the best way to write the query is:

select avg(case when response <> 'VERIFIED' then 1.0 else 0 end) as percentage
from validation v
where request_date BETWEEN (SYSDATE - 30/(24*60)) AND SYSDATE;

You can also write the where clause as:

where request_date between sysdate - interval '30' minute and sysdate