从一个选择中划分两个计数

时间:2013-10-21 12:53:35

标签: sql postgresql select percentage

我有一张这样的表:

date(timestamp) Error(integer)   someOtherColumns

我有一个查询来选择特定日期的所有行:

SELECT * from table
WHERE date::date = '2010-01-17'  

现在我需要计算Error等于0(从那天起)的所有行,并将其除以所有行的数量(从那天起)。

所以结果看起来应该是这样的

Date(timestamp)      Percentage of failure
2010-01-17           0.30

数据库非常庞大,数百万行......

如果有人知道如何更长时间地执行此操作,那将会很棒 - 从一天到另一天的间隔。

Date(timestamp)      Percentage of failure
2010-01-17           0.30
2010-01-18           0.71
and so on

4 个答案:

答案 0 :(得分:4)

这个怎么样(如果error只能是1和0):

select
   date,
   sum(Error)::numeric / count(Error) as "Percentage of failure"
from Table1
group by date

或者,如果error可以是任何整数:

select
   date,
   sum(case when Error > 0 then 1 end)::numeric / count(Error) as "Percentage of failure"
from Table1
group by date

我只计算not 0(假设错误是错误!= 0时),并且没有将空值记入帐户(不知道你想怎么对待它)。所以这是另一个查询,它将空值视为0,并以两种相反的方式计算失败百分比:

select
    date,
    round(count(nullif(Error, 0)) / count(*) ::numeric , 2) as "Percentage of failure",
    1- round(count(nullif(Error, 0)) / count(*) ::numeric , 2) as "Percentage of failure2"
from Table1
group by date
order by date;

<强> sql fiddle demo

答案 1 :(得分:2)

试试这个

select cast(data1.count1 as float)/ cast(data2.count2 as float) 
 from (
select count(*) as count1 from table date::date = '2010-01-17' and Error = 0) data1, 

(select count(*) as count1 from table date::date = '2010-01-17') data2

答案 2 :(得分:1)

SELECT date
     , round(count((error = 0) OR NULL) / count(*)::numeric, 2) AS percent_fail
FROM   tbl
GROUP  BY 1
ORDER  BY 1;

如果error可以是NULL,这甚至有用。

-> SQLfiddle demo.

在这个密切相关的问题下,更多(包括对绩效的影响): Compute percents from SUM() in the same SELECT sql query

this related answer on dba.SE计算方式的比较和基准。

答案 3 :(得分:0)

您可以使用generate_series并从那里获取它。

像这样:

WITH CTE AS 
(
     SELECT 
         m
        --,extract('year'  FROM m) AS theyear
        --,extract('month' FROM m) AS themonth
        --,extract('day' FROM m) AS theday

        ,(SELECT COUNT(*) AS cnt FROM  table WHERE date::date = m AND Error = 1) AS data1 
        ,(SELECT COUNT(*) AS cnt FROM  table WHERE date::date = m) AS data2 
    FROM  
    (
        SELECT generate_series('2012-04-01'::date, '2016-01-01'::date, interval '1 day') AS m
    ) AS g 
) -- END OF CTE 

SELECT 
      m
     ,COALESCE(data1 * 100.0 / NULLIF(data2, 0.0), 0.0) AS ErrorPercentage
FROM CTE

详情请见: How to perform a select query in a DO block?