我们希望获得本月和上个月的总销售额。查询是:
SELECT sum(CASE
WHEN date_trunc('month', date_start)= date_trunc('month', now()) THEN sales
ELSE NULL
END) AS curr_sales,
sum(CASE
WHEN date_trunc('month', date_start)= date_trunc('month', now()- interval '1' MONTH) THEN sales
ELSE NULL
END) AS pr_sales
FROM sales
但是它返回了此错误:
“ Redshift表不支持指定的类型或函数(每个INFO消息一个)。我们正在运行Postgresql 8.0.2。有什么想法吗?谢谢!
答案 0 :(得分:2)
使用date_trunc('month', current_date)
而不是date_trunc('month', now())
获取当前月份的红移。
now()在redshift中不受支持,但是current_date将以默认格式YYYY-MM-DD返回当前会话时区(默认为UTC)中的日期。
更新
使用示例数据查询>>
with sales(sales, date_start) as(
select 1 , current_date union
select 2 , current_date union
select 2 , current_date - interval '1' month union
select 3 , current_date - interval '1' month
)
SELECT sum(CASE
WHEN date_trunc('month', date_start)= date_trunc('month', current_date) THEN sales
ELSE NULL
END) AS curr_sales,
sum(CASE
WHEN date_trunc('month', date_start)= date_trunc('month', current_date - interval '1' MONTH) THEN sales
ELSE NULL
END) AS pr_sales
FROM sales;
结果如预期般到来:
curr_sales pr_sales
3 5