2

我收到此错误:

ERROR:  column "errors" does not exist  
LINE 11: where errors >= 1

即使我用选择检查了结果。

我正在使用 postgresql 服务器,我有一个名为 log 的表,如下所示:

Column |           Type           |                    Modifiers                     
--------+--------------------------+--------------------------------------------------
 path   | text                     | 
 ip     | inet                     | 
 method | text                     | 
 status | text                     | 
 time   | timestamp with time zone | default now()
 id     | integer                  | not null default nextval('log_id_seq'::regclass)

数据库中有很多行。

这是我的查询

select 
    a.date, 
    (cast(a.count as decimal) * 100 / b.count) as errors 
from (
    select date(time) as date, count(status) from log
    where status!='200 OK'
    group by date
    order by date asc
) as a 
join (
    select date(time) as date, count(status) 
    from log
    group by date
    order by date asc
) as b on a.date=b.date
where errors >= 1
order by errors desc;

但是当我在没有“where errors>=1”的情况下尝试此查询时,我得到了预期的结果,其中包含 2 列,一个命名日期,另一个命名错误

这是结果

select 
    a.date, 
    (cast(a.count as decimal) * 100 / b.count) as errors 
from (
    select date(time) as date, count(status) from log
    where status!='200 OK'
    group by date
    order by date asc
) as a 
join (
    select date(time) as date, count(status) 
    from log
    group by date
    order by date asc
) as b on a.date=b.date
order by errors desc;

结果:

    date    |         errors         
------------+------------------------
 2016-07-17 |     2.2626862468027260
 2016-07-19 | 0.78242171265427079381
 2016-07-24 | 0.78221415607985480944
 2016-07-05 | 0.77493816982687551525
 2016-07-06 | 0.76678716179209113813
4

3 回答 3

1

替换where errors >= 1为,(cast(a.count as decimal) * 100 / b.count)>=1因为没有称为错误的列,而是派生列:

select a.date, (cast(a.count as decimal) * 100 / b.count) as errors
  from (select date(time) as date, count(status)
          from log
         where status != '200 OK'
         group by date
         order by date asc) as a
  join (select date(time) as date, count(status)
          from log
         group by date
         order by date asc) as b
    on a.date = b.date
 where (cast(a.count as decimal) * 100 / b.count) >= 1
 order by errors desc; 

或者

它可以像上面一样使用,如下所示:

select *
  from (select a.date, (cast(a.count as decimal) * 100 / b.count) as errors
          from (select date(time) as date, count(status)
                  from log
                 where status != '200 OK'
                 group by date
                 order by date asc) as a
          join (select date(time) as date, count(status)
                 from log
                group by date
                order by date asc) as b
            on a.date = b.date) q
 where errors >= 1
 order by errors desc;

在子查询中。

于 2019-10-13T15:21:19.590 回答
0

您的查询将更简单地写为:

select l.time::date as dte,
       100 * avg( (status <> '200 OK')::int) as error_rate
from (
    from log l
    group by dte
    having 100 * avg( (status <> '200 OK')::int) >= 1
    order by error_rate desc;

having您可以在许多数据库的子句中使用别名,但不能在 Postgres 中使用。

于 2019-10-13T15:22:39.523 回答
0

代替

where errors >= 1

where (cast(a.count as decimal) * 100 / b.count) >= 1

您不能在查询的同一级别中使用列别名。

于 2019-10-13T15:22:53.697 回答