column does not exist error even when using the 'as' keyword [duplicate]

二次信任 提交于 2021-01-28 03:53:44

问题


I get this error:

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

even that i checked the result with select.

I'm using postgresql server, i have a table named log like the following:

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)

there are lots of lines in the db.

here is my query

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;

but when I try this query without the 'where errors>=1', I got the expected result with 2 columns one named date and another named errors

here is the result

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;

Result:

    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

回答1:


replace where errors >= 1 with (cast(a.count as decimal) * 100 / b.count)>=1 since there's no column called errors but a derived column :

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; 

OR

It may be used like above as below :

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;

within a subquery.




回答2:


Replace

where errors >= 1

with

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

You can't use column alias in the same level of your query.




回答3:


Your query would be more simply written as:

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;

You can use aliases in the having clause of many databases, but not Postgres.



来源:https://stackoverflow.com/questions/58364944/column-does-not-exist-error-even-when-using-the-as-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!