我收到此错误:
ERROR: column "errors" does not exist
LINE 11: where errors >= 1
即使我使用select检查了结果。
我正在使用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
答案 0 :(得分: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;
OR
它可以像上面一样使用,如下所示:
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;
在子查询中。
答案 1 :(得分: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中使用别名。
答案 2 :(得分:0)
替换
where errors >= 1
使用
where (cast(a.count as decimal) * 100 / b.count) >= 1
您不能在查询的同一级别中使用列别名。