Postgres Subquery Group By Date

时间:2017-12-18 06:07:20

标签: postgresql

I have a simple log table and want to calculate an error rate per day. The data looks like this:

SELECT count(date(time)) as counts, date(time) as log_date, status 
FROM log 
GROUP BY log_date, status;

returns

 counts |  log_date  |    status
--------+------------+---------------
  38431 | 2016-07-01 | 200 OK
    274 | 2016-07-01 | 404 NOT FOUND
  54811 | 2016-07-02 | 200 OK
    389 | 2016-07-02 | 404 NOT FOUND

but I want something like

 errors |  log_date  | totals 
--------+------------+--------
    274 | 2016-07-01 |  38705
    389 | 2016-07-02 |  45845
    401 | 2016-07-01 |  54378

I've tried using a subquery, but that gives me a running total instead of a daily total, i.e.

SELECT count(*) as errors, date(e.time) as log_date, totals
FROM log e, (SELECT COUNT(*) as totals FROM log t) AS total_counts
WHERE e.status !='200 OK'
GROUP BY log_date, totals
ORDER BY log_date;

returns

 errors |  log_date  | totals
--------+------------+---------
    274 | 2016-07-01 | 1677735
    389 | 2016-07-02 | 1677735
    401 | 2016-07-03 | 1677735

and I get different errors if I try to use a GROUP BY within the subquery. I know I'm close, but I don't know how to make the outer and inner queries both group on the same day.

1 个答案:

答案 0 :(得分:2)

Use a case expression inside the aggregate function ("conditional aggregates")

SELECT
       date(time) as log_date
     , count(case when status <> '200 OK' then 1 end) as errors
     , count(*) as totals
FROM log
GROUP BY
       date(time)