Postgresql 将当前数据和一年前同一日期的数据进行比较(Postgres)
我试图返回当前日期的订单数据和一年前同一日期的订单数据。 我的想法是创建两个类似的表,并通过添加WHERE子句来合并日期。但这似乎不起作用。 你能看一下我的代码,看看你是否发现了什么错误吗? 我的结果是完全空白的。 非常感谢Postgresql 将当前数据和一年前同一日期的数据进行比较(Postgres),postgresql,Postgresql,我试图返回当前日期的订单数据和一年前同一日期的订单数据。 我的想法是创建两个类似的表,并通过添加WHERE子句来合并日期。但这似乎不起作用。 你能看一下我的代码,看看你是否发现了什么错误吗? 我的结果是完全空白的。 非常感谢 WITH orders_channels AS (SELECT 'BLH' AS brand, date_trunc('week', date)::date AS date, channel, order_type, case w
WITH
orders_channels AS
(SELECT
'BLH' AS brand,
date_trunc('week', date)::date AS date,
channel,
order_type,
case when (date_trunc('week', date)::date = current_date - interval '1 day') then 'current'
else 'previous' end
as week_type,
sum(orders) AS orders
FROM
de_data.orders_daily_channel_attribution_dashboard
WHERE
date > date_trunc('day', current_date) - interval '1 day'
GROUP BY 1,2,3,4),
wow_orders_channels AS
(SELECT
'BLH' AS brand,
date_trunc('week', date)::date AS date,
channel,
order_type,
case when (date_trunc('week', date)::date = current_date - interval '1 day') then 'current'
else 'previous' end
as week_type,
sum(orders) AS orders
FROM
de_data.orders_daily_channel_attribution_dashboard
WHERE
date >= date_trunc('week', current_date) - INTERVAL '1 year'
GROUP BY 1,2,3,4)
SELECT
*
FROM
(SELECT
o.brand,
date_trunc('week', o.date)::date as week,
'SEO_ACQ' AS name,
o.orders,
wow.orders as wow_orders
FROM
orders_channels o
join wow_orders_channels wow on wow.date = o.date - interval '1 year' and o.order_type = wow.order_type
where
o.channel = 'SEO'
AND o.order_type = 'ACQUISITION'
UNION ALL
SELECT
o.brand,
date_trunc('week', o.date) as week,
'CRM_ORDERS' AS name,
SUM(o.orders),
sum(wow.orders) as wow_orders
FROM
orders_channels o
join wow_orders_channels wow on wow.date = o.date - interval '1 year' and o.order_type = wow.order_type
WHERE
o.channel = 'CRM'
GROUP BY 1,2,3) x
ORDER BY 3,2
你想怎么处理闰年?我还没想过呢。我仍在努力使其正常工作:)如果您提供相关表的架构、一些示例数据以及该数据预期的输出,可能会有所帮助。缺少基本信息:您的Postgres版本、表定义。而且
似乎不起作用
既不是有效的错误消息,也不是对错误的有用描述。请编辑问题以澄清。