即使使用 'as' 关键字,列也不存在错误
column does not exist error even when using the 'as' keyword
我收到此错误:
ERROR: column "errors" does not exist
LINE 11: where errors >= 1
即使我用 select 检查了结果。
我正在使用 postgresql 服务器,我有一个 table 如下所示的命名日志:
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
将 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;
在子查询中。
您的查询可以更简单地写成:
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 不行。
替换
where errors >= 1
和
where (cast(a.count as decimal) * 100 / b.count) >= 1
您不能在同一级别的查询中使用列别名。
我收到此错误:
ERROR: column "errors" does not exist
LINE 11: where errors >= 1
即使我用 select 检查了结果。
我正在使用 postgresql 服务器,我有一个 table 如下所示的命名日志:
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
将 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;
在子查询中。
您的查询可以更简单地写成:
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 不行。
替换
where errors >= 1
和
where (cast(a.count as decimal) * 100 / b.count) >= 1
您不能在同一级别的查询中使用列别名。