Sql Amazon红移中的语法错误
我有一个语法错误,一个小时内无法解决: 错误:42601:语法错误位于或接近“)” 我尝试在with闭包中运行select语句,它按预期运行。 我想错误在Sql Amazon红移中的语法错误,sql,amazon-redshift,Sql,Amazon Redshift,我有一个语法错误,一个小时内无法解决: 错误:42601:语法错误位于或接近“)” 我尝试在with闭包中运行select语句,它按预期运行。 我想错误在选择2198作为客户站点下的某个地方, 我使用MySql逻辑(除了带有闭包的),所以我的错误可能就源于此。 有人吗?我将IF转换为CASE,错误再也没有出现: with all_comp_prices as ( SELECT distinct m2198.to_product_id AS competitor_pro
选择2198作为客户站点下的某个地方,
我使用MySql逻辑(除了带有闭包的),所以我的错误可能就源于此。
有人吗?我将IF
转换为CASE
,错误再也没有出现:
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
既然可以使用ISO日期或时间戳,为什么还要使用epoch?它使编码和调试变得更加容易。阅读手册也有帮助…我使用亚马逊红移,这里是它的文档:我的错,我没有提到它(编辑了问题)。由于红移是基于Postgresql的,所以语法是完全相同的。getdate()
证明了它不完全相同。另外:红移是基于一个非常旧的Postgres版本。
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