sql - 潜在销售损失查询
问题描述
我正在构建一个仪表板来显示电子商务公司的潜在销售损失。潜在的销售损失是当客户订购产品但当时我们的库存中没有该产品时。为此,我使用了变量产品 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;
如果我的逻辑或查询有误,请告诉我。
解决方案
推荐阅读
- react-native - 每当我尝试在移动设备上构建我的 react-native 应用程序时,都会出现“应用程序已停止”消息
- jquery - 如何在按钮单击时获得最后一个复选框选中的值
- xml - 如何进行 XSLT 转换?
- ios - iPhone位置的角度
- angular - 将带有布尔值的对象 (json) 添加到离子存储 (ionic 4)
- c# - 使用 Cosmos DB 时使用带有谓词 SQL 注入的 CreateDocumentQuery 是否安全?
- node.js - 无法创建多个 mongo 连接
- rust - 为什么许多原始类型都有一个“Atomic*”类型而不是通用的“Atomic”
`? - c# - 格式化asp表格单元格文本
- swift - 无法推断通用参数“主题”