2016-03-30 13 views
0

私は今、時間解決できない構文エラーがあります。構文エラー

ERROR: 42601: syntax error at or near ")"

with all_comp_prices as 
(
    SELECT distinct 
     m2198.to_product_id AS competitor_product_id, 
     h_comp.when_seen, 
     h_comp.when_seen/86400 AS day, 
     h_comp.price 
    FROM 
     tbl_productmatch_2198 as m2198 
     JOIN 
     (
     select * from tbl_producthistory_2414 
     union select * from tbl_producthistory_2415 
     -- insert union more competitors here 
     ) 
     as h_comp 

     ON h_comp.product_id = m2198.to_product_id 
    WHERE 
     h_comp.when_seen >= extract(epoch from (getdate() - INTERVAL '7 DAYS')) 
) 
select 2198 as customer_site, 
TIMESTAMP 'epoch' + lt.day*86400 * INTERVAL '1 second' as date, 
sum(if(ap.price is null,1,0)) as no_price_competitors_products_count, 
sum(if(ap.price is null,1,0))/1 as ratio_to_total_competitors_products 
from 
(
    select acp.competitor_product, max(acp.when_seen) latest_time, acp.day from 
    all_comp_prices as acp 
    group by acp.competitor_product, acp.day 
) as lt -- latest times for each product per day 
join 
(
    all_comp_prices as ap -- all prices for all times 
) 
on lt.latest_time=ap.when_seen and lt.competitor_product=ap.competitor_product 
group by lt.day 
; 

を私は閉鎖して内部のselect文を実行しようと期待どおりに動作しますが。 は私が(WITHクロージャを除く)のMySQLロジックで行くので、多分私のミスは、その由来

...ミスがselect 2198 as customer_site,の下のどこかにあると仮定します。 誰でも?

+1

を使用すると、ISOの日付とタイムスタンプを使用することができたときにエポックを扱うのはなぜ?コーディングとデバッグが非常に簡単になります。マニュアルを読むことはまた助けになります... –

答えて

0

私はCASEIFを変換し、エラーが戻ってきたことはありません:

with all_comp_prices as 
(
    SELECT distinct 
     m2198.to_product_id AS competitor_product_id, 
     h_comp.when_seen, 
     h_comp.when_seen/86400 AS day, 
     h_comp.price 
    FROM 
     tbl_productmatch_2198 as m2198 
     JOIN 
     (
     select * from tbl_producthistory_2414 
     union select * from tbl_producthistory_2415 
     -- insert union more competitors here 
     ) 
     as h_comp 

     ON h_comp.product_id = m2198.to_product_id 
    WHERE 
     h_comp.when_seen >= extract(epoch from (getdate() - INTERVAL '7 DAYS')) 
) 
select 2198 as customer_site, 
TIMESTAMP 'epoch' + lt.day*86400 * INTERVAL '1 second' as date, 
sum(CASE WHEN ap.price is null THEN 1 else 0 END) as no_price_competitors_products_count, 
from 
(
select acp.competitor_product_id, max(acp.when_seen) latest_time, acp.day from 
    all_comp_prices as acp 
    group by acp.competitor_product_id, acp.day 
) as lt -- latest times for each products per day 
join 
all_comp_prices as ap -- all prices for all times 
on lt.latest_time=ap.when_seen and lt.competitor_product_id=ap.competitor_product_id 
group by date 
+0

私はAmazon Redshiftと仕事をしており、そのドキュメントはこちらです:http://docs.aws.amazon.com/redshift/latest/dg/r_GETDATE.html –

+0

with PostgreSQL –

+0

私のせいでは言及しませんでした(質問を編集しました)。 RedshiftはPostgresqlに基づいているので、構文はまったく同じだと考えました。 –