2017-09-25 33 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,因此將其添加到選擇列表中。