首页 > 解决方案 > 潜在销售损失查询

问题描述

我正在构建一个仪表板来显示电子商务公司的潜在销售损失。潜在的销售损失是当客户订购产品但当时我们的库存中没有该产品时。为此,我使用了变量产品 ID、数量、购买数量、out_of_stock(最近库存中的产品),以小时(0-24)为单位,时间 order_placed_time。我的逻辑是基于 product_id 和 where 子句(缺货时间为 0)的产品聚合数量,之后我将值与 where(缺货不是 0)和平均值的子句进行比较基于每天的产品数量之间的差异将为我们提供我们丢失的数量。然后将该值乘以该特定日期的产品数量/费率,以获得潜在的销售损失。

使用的查询

with CTE_1 as(select * from (select darkstore_product_id,
       out_of_stock,
       express_out_of_stock,
       manually_stock_out_time,
       log_date
from daily_product_stock_logs
where out_of_stock > 0 or express_out_of_stock > 0 or manually_stock_out_time is not null) b
where b.log_date > current_date - interval '60 days'),
CTE_2 as (select product_id,actual_price,description,Date from (select product_id,
       round(avg(amount/qty)) as actual_price,
       date(order_line_items.created_at::timestamp AT time zone INTERVAL '-05:30') as Date
from order_line_items
group by Date,order_line_items.product_id) b
left join product_details using(product_id)
where b.Date > current_date - interval '60 days')
select * from CTE_1 left join CTE_2 on CTE_1.darkstore_product_id = CTE_2.product_id;

如果我的逻辑或查询有误,请告诉我。

标签: sqlpostgresql

解决方案


推荐阅读