PostgreSQL:如何在子查询中返回多行并为每一行查找百分比?

时间:2018-07-02 11:49:18

标签: postgresql

该查询应该查找在特定日期发生的错误的百分比,但我在所有日期都得到相同的百分比。有什么方法可以从子查询返回多行。

select date(time) as day , ((select date(time) as day, from log where status != '200 OK')/(select date(time) as day from log )) as views from log group by day;

2 个答案:

答案 0 :(得分:0)

尝试一下:

select
  date(time) as day,
  ((count(*) filter(where status!=200)) / 
  count(*)::decimal(5,2))::decimal(5,2)
from log
group by day

答案 1 :(得分:0)

尝试一下

select date(time) as day , count(1) / tot_err as views
From log, (Select count(1) as tot_err from log where status != '200 OK') 
Where status != '200 OK'
group by day;