Postgres选择语句,列不存在

时间:2019-10-25 23:36:50

标签: sql postgresql column-alias

ERROR:  column "countis" does not exist

ERROR:  column "percentage" does not exist

发生错误

我正在犯什么错误,大概不是一个错误那么复杂。

    select avclassfamily as avclass
        , count(*) as countis,
        sum(case when avclassfamily = avclassfamily and lolbin=True then 1 else 0 end) as lolbinnumber
        , 
        case sum(case when avclassfamily = avclassfamily and lolbin=True then 1 else 0 end) when 0 then NULL else (sum(case when avclassfamily = avclassfamily and lolbin=True then 1 else 0 end) * 100 / count(*) ) end as percentage

    from malwarehashesandstrings where behaviouralbinary=True and countis >= 10 and percentage < 10
    group by avclassfamily 
    order by percentage desc NULLS LAST;

表的数据库架构为:

    CREATE TABLE malwarehashesandstrings (
    malware_id bigserial primary key,
    malwarehashmd5 varchar(1000) default NULL,
    malwarehashsha1 varchar(1000) default NULL,
    malwarehashsha256 varchar(1000) default NULL,
    virustotalstring text NOT NULL
    );





    ALTER TABLE malwarehashesandstrings ADD COLUMN v3api boolean default false;
    ALTER TABLE malwarehashesandstrings ADD COLUMN numpositives SMALLINT default NULL;
    ALTER TABLE malwarehashesandstrings ADD COLUMN avclassfamily varchar(1000) default NULL;


    ALTER TABLE malwarehashesandstrings ADD COLUMN lolbins boolean default false;
    ALTER TABLE malwarehashesandstrings ADD COLUMN lolbintype varchar(1000) default NULL;
    ALTER TABLE malwarehashesandstrings ADD COLUMN origindataset varchar(1000) default NULL;

希望这有助于诊断

1 个答案:

答案 0 :(得分:0)

该问题在以下博客文章中得到了说明:

https://www.postgresql.org/message-id/18195.1038530280@sss.pgh.pa.us

实际使用的查询如下所示,这是因为查询的where部分是在select之前进行处理的。