SELECT a.*, b.predict_day FROM dw.ol_autotask_model_channels_predict_content a JOIN ( SELECT cust_id, dt, SUM(CASE WHEN predict IN ('other', 'middle') THEN 0 ELSE 1 END) AS predict_day FROM dw.ol_autotask_model_channels_predict_content WHERE dt BETWEEN '20230601' AND '20230630' GROUP BY cust_id, dt ) b ON a.cust_id = b.cust_id AND a.dt = b.dt WHERE b.predict_day = 0 and a.dt BETWEEN '20230601' AND '20230630' ;
时间: 2024-04-08 13:28:28 浏览: 89
plot_cv_predict.zip_cross_val_predict_plot(cv._predict-python_预测
5星 · 资源好评率100%
你的查询看起来是正确的,但是在这种情况下,你可以进一步简化查询语句。以下是优化后的查询代码:
```sql
SELECT a.*, b.predict_day
FROM dw.ol_autotask_model_channels_predict_content a
JOIN (
SELECT cust_id, dt, COUNT(*) AS predict_day
FROM dw.ol_autotask_model_channels_predict_content
WHERE dt BETWEEN '20230601' AND '20230630' AND predict NOT IN ('other', 'middle')
GROUP BY cust_id, dt
HAVING COUNT(*) = 0
) b ON a.cust_id = b.cust_id AND a.dt = b.dt
WHERE a.dt BETWEEN '20230601' AND '20230630';
```
在上述查询中,我们使用了一个内部子查询来计算 `predict_day`,并通过 `HAVING COUNT(*) = 0` 条件筛选出 `predict_day` 为 0 的记录。同时,我们将 `predict NOT IN ('other', 'middle')` 条件移至子查询中,以进一步简化查询。
请注意,上述优化仅是基于你提供的查询进行的。根据实际数据量和索引情况,可能还有其他优化方法可供考虑,例如添加适当的索引或分区策略。
阅读全文