2017-09-25 58 views
0

我使用滞后函数将两个表连接在一起,以便第一个表不显示重复的度量行。使用LAG并比较列

但是,我在预先选择连接时遇到了引用滞后列的问题。

我的查询如下,提到的列标有!!!

select "Campaign", 
"Ad group" , 
"Final URL" , 
"Headline 1" , 
"Headline 2" , 
"Description" , 
"Path 1" , 
"Path 2" , 
"Status" , 
"Labels" , 
    case when prev_key is null or prev_key != "Key" then "Clicks" end as 
    "Clicks", 
    case when prev_key is null or prev_key != "Key" then "Impressions" end 
    as 
    "Impressions", 
     case when prev_key is null or prev_key != "Key" then "Cost" end as 
    "Cost", 
    case when prev_key is null or prev_key != "Key" then "Avg. position" end 
as 
    "Avg. position", 
    case when prev_key is null or prev_key != "Key" then "Initial Leads" end 
    as 
    "Initial Leads", 
    case when prev_key is null or prev_key != "Key" then "Evaluations" end 
    as 
    "Evaluations", 
    case when prev_key is null or prev_key != "Key" then "Won Leads" end as 
    "Won Leads", 
    case when prev_key is null or prev_key != "Key" then "Opportunities" end 
    as 
    "Opportunities", 
    "Language", 
    "Network", 
    "Main Keyword", 
    "Cluster Keyword 1", 
    "Match Type" 


    from 

    (SELECT 
    "x"."Campaign", 
    "x"."Ad group", 
    "x"."Final URL", 
"x"."Headline 1", 
"x"."Headline 2", 
"x"."Description", 
"x"."Path 1", 
"x"."Path 2", 
"x"."Status", 
"x"."Labels", 
!!!lag ("x"."Key") over() AS prev_Key!!!, 
"x"."Clicks", 
"x"."Impressions", 
"x"."Cost", 
"x"."Avg. position", 
"x"."Initial Leads", 
"x"."Evaluations", 
"x"."Won Leads", 
"x"."Opportunities", 
"x"."Language", 
    "x"."Network", 
    "x"."Main Keyword", 
    "x"."Cluster Keyword 1", 
    "x"."Match Type" 


    FROM ad_copies_final_joined_concatenated x join 
    ad_copies_final_to_join_concatenated 

    using ("Key") 

    order by "Campaign" desc, "Key" 
    ) sub ; 

输出错误信息如下;

ERROR: column "Key" does not exist LINE 11: case when prev_key is null or prev_key != "Key" then "Cli... ^

回答

0

不应该对字符串和列名使用相同的语法。

我怀疑这个错误是由于对字符串使用了双引号引起的。

尝试将所有不是列的内容更改为单引号标记。

E.G.

case when prev_key is null or prev_key != 'Key' then "Avg. position" end 
as "Avg. position" 

或者,你竟然想和Key列比较prev_key列。在这种情况下,您忘记在内部查询中选择Key,因此将其添加到选择列表中。